Save multiple fields using ACF in Woocommerce(cart, checkout, order details) - woocommerce

I'm setting up ACF for selecting options in single product page. I already added the custom fields in single product page. How can I save multiple fields?
This code is working, but I need to save multiple fields like on the added field option-color.
/** Output custom fields field. */
function product_options_output_field() {
$html = "";
if( have_rows('select_options') ):
$html .= "<div class='select-options'>";
$html .= "<h4>Select Options</h4>";
$html .= "<select class='option-multi-trip-one-way' id='option-multi-trip-one-way' name='option-multi-trip-one-way'>";
$html .= "<option value='N/A'>Multi-trip/One-way **</option>";
while( have_rows('select_options') ) : the_row();
$multi_trip = get_sub_field('multi-tripone-way');
$html .= "<option value=".$multi_trip.">".$multi_trip."</option>";
endwhile;
$html .= "</select>";
$html .= "<br/>";
$html .= "<select class='option-color' id='option-color' name='option-color'>";
$html .= "<option value='N/A'>Color</option>";
while( have_rows('select_options') ) : the_row();
$color = get_sub_field('color');
$html .= "<option value=".$color.">".$color."</option>";
endwhile;
$html .= "</select>";
$html .= "</div>";
endif;
echo $html;
}
add_action( 'woocommerce_before_add_to_cart_button', 'product_options_output_field', 10 );
/** Add custom fields to cart item. */
function product_options_add_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
$option_multi_trip_one_way = filter_input( INPUT_POST, 'option-multi-trip-one-way');
if ( empty( $option_multi_trip_one_way ) ) {
return $cart_item_data;
}
$cart_item_data['option-multi-trip-one-way'] = $option_multi_trip_one_way;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'product_options_add_to_cart_item', 10, 3 );
/**Display custom fields in the cart. */
function product_options_display_cart( $item_data, $cart_item ) {
if ( empty( $cart_item['option-multi-trip-one-way'] ) ) {
return $item_data;
}
$item_data[] = array(
'key' => __( 'Multi-trip/One-way **' ),
'value' => wc_clean( $cart_item['option-multi-trip-one-way'] ),
'display' => '',
);
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'product_options_display_cart', 10, 2 );
/** Add custom fields to order. */
function product_options_to_order_items( $item, $cart_item_key, $values, $order ) { if ( empty( $values['option-multi-trip-one-way'] ) ) {
return; } $item->add_meta_data( __( 'Multi-trip/One-way **' ), $values['option-multi-trip-one-way'] );}add_action( woocommerce_checkout_create_order_line_item', 'product_options_to_order_items', 10, 4 );
http://prntscr.com/n42nig http://prntscr.com/n42nz0 http://prntscr.com/n42oj5

/**Output ACF in single product. */
function product_options_output_field() {
$html = "";
if( have_rows('select_options') ):
$html .= "<div class='select-options'>";
$html .= "<h4>Select Options</h4>";
$html .= "<select class='option-multi-trip-one-way' id='option-multi-trip-one-way' name='option-multi-trip-one-way'>";
$html .= "<option value='N/A'>Multi-trip/One-way **</option>";
while( have_rows('select_options') ) : the_row();
$multi_trip = get_sub_field('multi-tripone-way');
$html .= "<option value=".$multi_trip.">".$multi_trip."</option>";
endwhile;
$html .= "</select>";
$html .= "<br/>";
$html .= "<select class='option-color' id='option-color' name='option-color'>";
$html .= "<option value='N/A'>Color</option>";
while( have_rows('select_options') ) : the_row();
$color = get_sub_field('color');
$html .= "<option value=".$color.">".$color."</option>";
endwhile;
$html .= "</select>";
$html .= "</div>";
endif;
echo $html;
}
add_action( 'woocommerce_before_add_to_cart_button', 'product_options_output_field', 10 );
/** * Add ACF to cart item. */
function product_options_add_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
$option_multi_trip_one_way = filter_input( INPUT_POST, 'option-multi-trip-one-way');
$option_color = filter_input( INPUT_POST, 'option-color');
if ( empty( $option_multi_trip_one_way) || empty( $option_color)) {
return $cart_item_data;
}
$cart_item_data['option-multi-trip-one-way'] = $option_multi_trip_one_way;
$cart_item_data['option-color'] = $option_color;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'product_options_add_to_cart_item', 10, 3 );
/** Display ACF in the cart.*/
function product_options_display_cart( $item_data, $cart_item ) {
if ( empty( $cart_item['option-multi-trip-one-way'] ) || empty( $cart_item['option-color'] )) {
return $item_data;
}
$item_data[] = array(
'key' => __( 'Multi-trip/One-way **' ),
'value' => wc_clean( $cart_item['option-multi-trip-one-way'] ),
'display' => '',
);
$item_data[] = array(
'key' => __( 'Color' ),
'value' => wc_clean( $cart_item['option-color'] ),
'display' => '',
);
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'product_options_display_cart', 10, 2 );
/** Add ACF to order.*/
function product_options_to_order_items( $item, $cart_item_key, $values, $order ) {
if ( empty( $values['option-multi-trip-one-way'] ) || empty( $values['option-color'] )) {
return;
}
$item->add_meta_data( __( 'Multi-trip/One-way **' ), $values['option-multi-trip-one-way'] );
$item->add_meta_data( __( 'Color' ), $values['option-color'] );
}
add_action( 'woocommerce_checkout_create_order_line_item', 'product_options_to_order_items', 10, 4 );

Related

Product review doesn't show woocommerce

Hi i enabled the review on woocommerce and doesn't show on products page and i put this code in snippets
function bbloomer_product_reviews_shortcode( $atts ) {
if ( empty( $atts ) ) return '';
if ( ! isset( $atts['id'] ) ) return '';
$comments = get_comments( 'post_id=' . $atts['id'] );
if ( ! $comments ) return '';
$html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';
foreach ( $comments as $comment ) {
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$html .= '<li class="review">';
$html .= get_avatar( $comment, '60' );
$html .= '<div class="comment-text">';
if ( $rating ) $html .= wc_get_rating_html( $rating );
$html .= '<p class="meta"><strong class="woocommerce-review__author">';
$html .= get_comment_author( $comment );
$html .= '</strong></p>';
$html .= '<div class="description">';
$html .= $comment->comment_content;
$html .= '</div></div>';
$html .= '</li>';
}
$html .= '</ol></div></div>';
return $html;
}
add_shortcode( 'woocommerce_after_single_product', 'bbloomer_product_reviews_shortcode' );
I checked the enable review to the all products and in woocommerce to and in the discussions
You can add a new tab using the action hook woocommerce_product_tabs. check the below code. code goes in your active theme functions.php file.
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
$tabs['products_review_tab'] = array(
'title' => __( 'Reviews', 'woocommerce' ),
'priority' => 120,
'callback' => 'products_review_tab_content'
);
return $tabs;
}
Now you can echo do_shortcode[] inside products_review_tab_content callback.
function products_review_tab_content() {
global $product;
echo do_shortcode( '[product_reviews_shortcode id="'.$product->id.'"]' );
}
function product_reviews_shortcode( $atts ) {
$atts = shortcode_atts( array(
'id' => ''
), $atts, 'product_reviews_shortcode' );
$comments = get_comments( array(
'post_id' => $atts['id']
) );
if ( ! $comments ) return '';
$html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';
foreach ( $comments as $comment ) {
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$html .= '<li class="review">';
$html .= get_avatar( $comment, '60' );
$html .= '<div class="comment-text">';
if ( $rating ) $html .= wc_get_rating_html( $rating );
$html .= '<p class="meta"><strong class="woocommerce-review__author">';
$html .= get_comment_author( $comment );
$html .= '</strong></p>';
$html .= '<div class="description">';
$html .= $comment->comment_content;
$html .= '</div></div>';
$html .= '</li>';
}
$html .= '</ol></div></div>';
return $html;
}
add_shortcode( 'product_reviews_shortcode', 'product_reviews_shortcode' );
Tested and works.

Woocommerce - How to add one link "add to cart" to the loop product thumbnail?

I've tried several things adding the code to functions.php.
I think that the time I've been closer was with this code:
printf('<a class="button" href=" ..... "?>');
echo woocommerce_get_product_thumbnail();
printf('</a>');
}
But I don't know what to href on there.
previously, I removed the other liks with:
/*remove links to loop, add link to title*/
remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10);
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5);
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_open', 5);
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_close', 20);
function woocommerce_template_loop_product_title() {
echo sprintf('<h2 class="woocommerce-loop-product__title"><a title="%2$s" href="%1$s">%2$s</a></h2>',
get_the_permalink(),
get_the_title()
);
}
Thank you very much
There may be some modifications that need to be made for it to fit your scenario.
Add the following code to your functions.php file.
This will only add the link to the thumbnail if you are on the products page.
function add_link_woocommerce_single_product_image_thumbnail_html( $html, $post_thumbnail_id ) {
/**
* This will only add the link on a single product page.
*/
if ( is_product() ) :
global $product;
$product_id = $product->get_id();
$product_url = get_permalink( $product_id );
$main_image = true;
$flexslider = (bool) apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) );
$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
$thumbnail_size = apply_filters( 'woocommerce_gallery_thumbnail_size', array( $gallery_thumbnail['width'], $gallery_thumbnail['height'] ) );
$image_size = apply_filters( 'woocommerce_gallery_image_size', $flexslider || $main_image ? 'woocommerce_single' : $thumbnail_size );
$full_size = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' ) );
$thumbnail_src = wp_get_attachment_image_src( $post_thumbnail_id, $thumbnail_size );
$full_src = wp_get_attachment_image_src( $post_thumbnail_id, $full_size );
$alt_text = trim( wp_strip_all_tags( get_post_meta( $post_thumbnail_id, '_wp_attachment_image_alt', true ) ) );
$image = wp_get_attachment_image(
$post_thumbnail_id,
$image_size,
false,
apply_filters(
'woocommerce_gallery_image_html_attachment_image_params',
array(
'title' => _wp_specialchars( get_post_field( 'post_title', $post_thumbnail_id ), ENT_QUOTES, 'UTF-8', true ),
'data-caption' => _wp_specialchars( get_post_field( 'post_excerpt', $post_thumbnail_id ), ENT_QUOTES, 'UTF-8', true ),
'data-src' => esc_url( $full_src[0] ),
'data-large_image' => esc_url( $full_src[0] ),
'data-large_image_width' => esc_attr( $full_src[1] ),
'data-large_image_height' => esc_attr( $full_src[2] ),
'class' => esc_attr( $main_image ? 'wp-post-image' : '' ),
),
$post_thumbnail_id,
$image_size,
$main_image
)
);
$html = '<div data-thumb="' . esc_url( $thumbnail_src[0] ) . '" data-thumb-alt="' . esc_attr( $alt_text ) . '" class="woocommerce-product-gallery__image">' . $image . '</div>';
return $html;
endif;
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'add_link_woocommerce_single_product_image_thumbnail_html', 10, 2 );

woocommerce - Display Products in View orders page

I'm working on displaying the order products in the View Orders page in the customer account page like this
Can we do this?
Add this to your theme's 'functions.php'.
function add_products_my_account_orders_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
// add products after order total column
if ( 'order-total' === $key ) {
$new_columns['order-products'] = __( 'Products', 'textdomain' );
}
}
return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'add_products_my_account_orders_column' );
function add_products_data_my_account_orders_column( $order ) {
//loop through products of the order
foreach( $order->get_items() as $item_id => $item ) {
$product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$is_visible = $product && $product->is_visible();
$product_permalink = apply_filters( 'woocommerce_order_item_permalink', $is_visible ? $product->get_permalink( $item ) : '', $item, $order );
echo apply_filters( 'woocommerce_order_item_name', $product_permalink ? sprintf( '<p>%s', $product_permalink, $item['name'] ) : $item['name'], $item, $is_visible );
echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '× %s', $item['qty'] ) . '</strong></p>', $item );
}
}
add_action( 'woocommerce_my_account_my_orders_column_order-products', 'add_products_data_my_account_orders_column' );

How to get "items" column in orders panel in WooCommerce

Since WooCommerce major update 3.0+ the "Purchased" column in backend orders list panel has been removed. This column previously showed a toggle list of items in the order for quick viewing.
How to Get back this "items" column in orders panel?
If there is any hook for that? Any ideas?
Thanks
That appears to have been removed for performance reasons, but you could look at the code that was removed and add it back via the manage_shop_order_posts_columns filter and manage_shop_order_posts_custom_column action.
/**
* Modify the custom columns for orders.
* #param array $columns
* #return array
*/
function so_43719068_shop_order_columns( $columns ) {
// the new column as an array for subsequent array manip
$new_column = array( 'order_items' => __( 'Purchased', 'your-plugin' ) );
$insert_after = 'order_title';
// insert after specified column
if( isset( $columns[ $insert_after ] ) ){
// find the "title" column
$index = array_search( $insert_after, array_keys( $columns) );
// reform the array
$columns = array_merge( array_slice( $columns, 0, $index + 1, true ), $new_column, array_slice( $columns, $index, count( $columns ) - $index, true ) );
// or add to end
} else {
$columns = array_merge( $columns, $new_column );
}
return $columns;
}
add_filter( 'manage_shop_order_posts_columns', 'so_43719068_shop_order_columns', 20 );
/**
* Output custom columns for orders.
* #param string $column
* #param int $post_id
*/
function so_43719068_render_shop_order_columns( $column, $post_id ) {
global $the_order;
if ( empty( $the_order ) || $the_order->get_id() !== $post_id ) {
$the_order = wc_get_order( $post_id );
}
switch ( $column ) :
case 'order_items' :
/* translators: %d: order items count */
echo '' . apply_filters( 'woocommerce_admin_order_item_count', sprintf( _n( '%d item', '%d items', $the_order->get_item_count(), 'woocommerce' ), $the_order->get_item_count() ), $the_order ) . '';
if ( sizeof( $the_order->get_items() ) > 0 ) {
echo '<table class="show_order_items" cellspacing="0">';
foreach ( $the_order->get_items() as $item ) {
$product = apply_filters( 'woocommerce_order_item_product', $item->get_product(), $item );
$item_meta_html = wc_display_item_meta( $item, array( 'echo' => false ) );
?>
<tr class="<?php echo apply_filters( 'woocommerce_admin_order_item_class', '', $item, $the_order ); ?>">
<td class="qty"><?php echo esc_html( $item->get_quantity() ); ?></td>
<td class="name">
<?php if ( $product ) : ?>
<?php echo ( wc_product_sku_enabled() && $product->get_sku() ) ? $product->get_sku() . ' - ' : ''; ?><?php echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ); ?>
<?php else : ?>
<?php echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ); ?>
<?php endif; ?>
<?php if ( ! empty( $item_meta_html ) ) : ?>
<?php echo wc_help_tip( $item_meta_html ); ?>
<?php endif; ?>
</td>
</tr>
<?php
}
echo '</table>';
} else echo '–';
break;
endswitch;
}
add_action( 'manage_shop_order_posts_custom_column', 'so_43719068_render_shop_order_columns', 10, 2 );

WordPress add filter to wp_get_attachment_link

I need custom class for filter to wp_get_attachment_link. So what I so:
function modify_attachment_link( $markup ) {
global $post;
return str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link' );
It's work fine. But what I need to do in case if Link thumbnails to: Attachment Page
I mean I don't need a custom class at this case. Any help please?
And core function for wp_get_attachment_link is:
function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
$id = intval( $id );
$_post = & get_post( $id );
if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
return __( 'Missing Attachment' );
if ( $permalink )
$url = get_attachment_link( $_post->ID );
$post_title = esc_attr( $_post->post_title );
if ( $text )
$link_text = esc_attr( $text );
elseif ( $size && 'none' != $size )
$link_text = wp_get_attachment_image( $id, $size, $icon );
else
$link_text = '';
if ( trim( $link_text ) == '' )
$link_text = $_post->post_title;
return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );
}
So I mean if ( $permalink ) I don't need to add custom class for this function.
Try
function modify_attachment_link( $markup, $id, $size, $permalink ) {
global $post;
if ( ! $permalink ) {
$markup = str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
}
return $markup;
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link', 10, 4 );
That may work

Resources