Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce: Display also product variation description on cart items

I'm trying to display my product variation description in my Cart. I have tried inserting this code in the cart.php template:

if ( $_product->is_type( 'variation' ) ) {echo $_product->get_variation_description();}

By following this documentation https://docs.woocommerce.com/document/template-structure/

But it's still not showing up.

Not sure what I'm doing wrong here.

Can anyone help on this?

like image 936
simplycity Avatar asked Aug 19 '16 17:08

simplycity


2 Answers

Updated for WooCommerce version 3 and above

Since WooCommerce 3, get_variation_description() is now deprecated and replaced by get_description() WC_Product method.

To get your product item variation description (filtering variation product type condition), you can use the following hooked function instead:

// Cart page (and mini cart)
add_filter( 'woocommerce_cart_item_name', 'cart_item_product_description', 20, 3);
function cart_item_product_description( $item_name, $cart_item, $cart_item_key ) {
    if ( ! is_checkout() ) {
        if( $cart_item['variation_id'] > 0 ) {
            $description = $cart_item['data']->get_description(); // variation description
        } else {
            $description = $cart_item['data']->get_short_description(); // product short description (for others)
        }

        if ( ! empty($description) ) {
            return $item_name . '<br><div class="description">
                <strong>' . __( 'Description', 'woocommerce' ) . '</strong>: '. $description . '
            </div>';
        }
    }
    return $item_name;
}

// Checkout page
add_filter( 'woocommerce_checkout_cart_item_quantity', 'cart_item_checkout_product_description', 20, 3);
function cart_item_checkout_product_description( $item_quantity, $cart_item, $cart_item_key ) {
    if( $cart_item['variation_id'] > 0 ) {
        $description = $cart_item['data']->get_description(); // variation description
    } else {
        $description = $cart_item['data']->get_short_description(); // product short description (for others)
    }

    if ( ! empty($description) ) {
        return $item_quantity . '<br><div class="description">
            <strong>' . __( 'Description', 'woocommerce' ) . '</strong>: '. $description . '
        </div>';
    }

    return $item_quantity;
}

Now the description is just displayed between the title and the variation attributes values (if there is any).

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

like image 66
LoicTheAztec Avatar answered Sep 27 '22 18:09

LoicTheAztec


This will work for WC 3.0

    add_filter( 'woocommerce_cart_item_name', 'cart_variation_description', 20, 3);
function cart_variation_description( $title, $cart_item, $cart_item_key ) {
    $item = $cart_item['data'];

    if(!empty($item) && $item->is_type( 'variation' ) ) {
        return $item->get_name();
    } else
        return $title;
}
like image 20
Ger Groot Avatar answered Sep 27 '22 17:09

Ger Groot