简体   繁体   中英

Add product short description in cart Woocommerce

I would like to add short product description to cart: I do add it to the cart but whats weird that it does not show on checkout page, as my cart is in the header. Any ideas or other solutions are very helpful thanks in advance

function excerpt_in_cart() {

$excerpt = get_the_excerpt();
$excerpt = substr($excerpt, 0, 80);
return '<br><p class="shortDescription">' . $excerpt .'...' . '</p>';
}
add_action( 'woocommerce_cart_item_name', 'excerpt_in_cart', 40 );

When on checkout page it does not show this part from the code ' . $excerpt .' p shows up with the class just fine.

function excerpt_in_cart($cart_item_html, $product_data) {
global $_product;

$excerpt = get_the_excerpt($product_data['product_id']);
$excerpt = substr($excerpt, 0, 80);

echo $cart_item_html . '<br><p class="shortDescription">' . $excerpt . '...' . '</p>';
}

add_filter('woocommerce_cart_item_name', 'excerpt_in_cart', 40, 2);

First of all woocommerce_cart_item_name hook is filter hook not the action hook.

Most of the things you did correctly few minor issues are

  • You have to use add_filter with woocommerce_cart_item_name hook.
  • Overwrote the woocommerce created html instead of concatenate your excerpt.
  • Missed to handle each cart item's excerpt by using their product ID.

Additional info:

This is from wordpress core file plugin.php

function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    return add_filter($tag, $function_to_add, $priority, $accepted_args);
}

function add_action is just the wrapper function of add_filter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM