8

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?

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
simplycity
  • 83
  • 1
  • 1
  • 5
  • Hi @LoicTheAztec, thanks so much for the help. I just tried it today and it's not working for me. My programming knowledge is very limited so I apologize in advance for dumb mistakes. I inserted your code within my cart.php (see https://github.com/jessica16002002/Tega/blob/master/cart.php). Any chance you can take a look and help me find out what's wrong? Thanks!! – simplycity Aug 24 '16 at 22:24

3 Answers3

9

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.

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
  • @LoicTheAztec . Thanks for this code (Case 1). Should this still work in latest WC (3.x)? I tried it, and it's just showing "Description: " with nothing else. I'd love to get this working. – inspirednz Jun 03 '17 at 13:03
  • @LoicTheAztec Thanks! Just to update, for Case 1, replace $item->get_variation_description(); with $item->get_description(); for (now) current versions of WooCommerce. – Jason Aug 12 '17 at 21:13
  • @jason Thanks again, I have enhanced and updated the code. Is now compatible also with WooCommerce version 3+. – LoicTheAztec Aug 12 '17 at 23:35
3

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;
}
Ger Groot
  • 1,011
  • 10
  • 7
0

You can get it via global variable $woocommerce also-

global $woocommerce;
$cart_data = $woocommerce->cart->get_cart();
foreach ($cart_data as $value) {
    $_product = $value['data'];
    if( $_product->is_type( 'variation' ) ){
        echo $_product->id . '<br>';
    }
}

I already check it.

anik4e
  • 461
  • 7
  • 16
  • This is the old deprecated way… The new way since +/- 1 year is `WC()->cart` syntax without any need of `$global woocommerce;`… check the code on woocommerce templates or in core files, you will see. – LoicTheAztec Aug 19 '16 at 20:56