Add short product description to shopping cart Woocomerce

I would like to add a short description of the product to the cart: I add it to the cart, but it is strange that it does not appear on the checkout page since my cart is in the header. Any ideas or other solutions are very helpful 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 );

      

If on the checkout page it doesn't display that part from the code '. $ excerpt. 'p shows the class just fine.

+3


source to share


1 answer


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, a woocommerce_cart_item_name

hook is a filter hook, not an action hook.

Most of the things you did right are minor issues:

  • You need to use add_filter with woocommerce_cart_item_name

    hook.
  • Rewrote the woocommerce generated html instead of merging your snippet.
  • Failed to process excerpt of each basket using its product ID.



Additional Information:

This is from the wordpress core plugin.php file

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

      

A function add_action

is just a wrapper function add_filter

.

+2


source







All Articles