3

I'm struggling to find a simple solution to this problem I'm having with One-page-checkout plugin for Woocommerce.

My client would like to add the product description next to the product title in cart items.
Any thoughts on how I can manipulate the code to show the description?

This is what I have actually:

enter image description here

I would think that this plugin would just be hiding the description somewhere but I can't locate it anywhere in the code.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

2 Answers2

1

There is 2 ways to do it (making work for products and product variations):

1). With custom function hooked in woocommerce_get_item_data action hook (The best way):


add_filter( 'woocommerce_get_item_data', 'customizing_cart_item_data', 10, 2 );
function customizing_cart_item_data( $cart_data, $cart_item ) {
    $description = $cart_item['data']->get_description(); // Get the product description

    // For product variations when description is empty
    if( $cart_item['variation_id'] > 0 && empty( $description ) ){
        // Get the parent variable product object
        $parent_product = wc_get_product( $cart_item['product_id'] );
        // Get the variable product description
        $description = $parent_product->get_description();
    }

    // If product or variation description exists we display it
    if( ! empty( $description ) ){
        $cart_data[] = array(
            'key'      => __( 'Description', 'woocommerce' ),
            'value'    => $description,
            'display'  => $description,
        );
    }
    return $cart_data;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

… or …

2). With custom function hooked in woocommerce_cart_item_name filter hook:

WooCommerce: Display also product variation description on cart items

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
0

You could try this code in your functions.php located in your root of your theme folder. Not sure if it still works as I'm not active in Wordpress development anymore. [untested]

Updated: this should probably work for WooCommerce 3+

Using woocommerce_cart_item_name hook:

add_filter( 'woocommerce_cart_item_name', 'cart_description', 20, 3);
function cart_description( $name, $cart_item, $cart_item_key ) {
    // Get the corresponding WC_Product
    $product_item = $cart_item['data'];

    if(!empty($product_item)) {
        // WC 3+ compatibility
        $description = $product_item->get_description();
        $result = __( 'Description: ', 'woocommerce' ) . $description;
        return $name . '<br>' . $result;
    } else
        return $name;
    }
}
Lars Mertens
  • 1,389
  • 2
  • 15
  • 37