Show product image + name on checkout page - woocommerce

How can i put code in order to show both Picture and Image of my product??
function isa_woo_cart_attributes($cart_item, $cart_item_key) {
global $product;
if (is_cart()){
echo "<style>#checkout_thumbnail{display:none;}</style>";
}
$item_data = $cart_item_key['data'];
$post = get_post($item_data->id);
$thumb = get_the_post_thumbnail($item_data->id, array( 32, 50));
echo '<div id="checkout_thumbnail" style="float: left; padding-right: 8px">' . $thumb . '</div> ';
}
add_filter('woocommerce_cart_item_name', isa_woo_cart_attributes, 10, 2);
And
$product = new WC_product($item_data->id);
echo $product->name;
echo $product->price;

Replace your codes by the follows code snippet -
function isa_woo_cart_attributes( $product_name, $cart_item ) {
$_product = $cart_item['data'];
$product_permalink = $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '';
$thumbnail = $_product->get_image();
printf( '%s%s', esc_url( $product_permalink ), $thumbnail, $product_name );
}
add_filter('woocommerce_cart_item_name', 'isa_woo_cart_attributes', 99, 2);
Code goes to your active theme's functions.php

Related

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

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 );

How to add tags to code snippets?

I tried very hard during the day to add and tags but could not do as expected.
Can someone help me to add the tag and the tag in this code snippet?
echo wc_get_product_category_list( $product->get_id(), '<span class="meta-sep">,</span> ', '<span class="posted_in">' . _n( 'Category:', 'Categories:' , count( $product->get_category_ids() ), 'woocommerce' ) . ' ', '</span>' );
For example:
<th>Category:</th> <td>Kit baby</td>
echo '<th>SKU:</th>' . '<td>' . $product->get_sku() . '</td>';
Please help me thanks.
Display Product Tags:
<?php
$current_tags = get_the_terms( get_the_ID(), 'product_tag' );
if ( $current_tags && ! is_wp_error( $current_tags ) ) {
echo '<tr>';
echo '<th>Tags: </th>';
foreach ($current_tags as $tag) {
$tag_title = $tag->name;
$tag_link = get_term_link( $tag );
echo '<td>'.$tag_title.'</td>';
}
echo '</tr>';
} ?>
Display Product Category:
<?php
$current_tags = get_the_terms( get_the_ID(), 'product_cat' );
if ( $current_tags && ! is_wp_error( $current_tags ) ) {
echo '<tr>';
echo '<th>Category: </th>';
foreach ($current_tags as $tag) {
$tag_title = $tag->name;
$tag_link = get_term_link( $tag );
echo '<td>'.$tag_title.'</td>';
}
echo '</tr>';
} ?>
Hope this works for you.

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 );

How to get the vendor_ids for each of the vendors with products on the cart page in woocommerce

global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
I have used the following to get data about the products on the cart and display it. is it possible to get the vendor of each product too and how can I do that in woo commerce? again from the cart page because I want to echo the vendor address back under each product on the cart page.
add_filter( 'woocommerce_get_item_data', 'wc_add_address_to_cart', 10, 2 );
function wc_add_address_to_cart( $other_data, $cart_item ) {
$post_data = get_post( $cart_item['product_id'] );
$post_data->post_author;
$vendor_id = $post_data->post_author;
echo '<br>';
$Add = 'Address: ';
$city = 'City: ';
$tel = 'Phone: ';
echo $test;
$user_id = $vendor_id;
// Get all user meta data for $user_id
$meta = get_user_meta( $user_id );
// Filter out empty meta data
$meta = array_filter( array_map( function( $a ) {
return $a[0];
}, $meta ) );
echo $Add;
print_r( $meta ['_vendor_address_1'] );
echo '<br>';
echo $city;
print_r( $meta['_vendor_city'] );
echo '<br>';
echo $tel;
print_r( $meta['_vendor_phone'] );
return $other_data;
}
Finally got what I was looking for. Just add that to the functions.php

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