Woocommerce: displaying product descriptions on a page in a basket

I am trying to display my product description in my cart. I tried pasting this code into the cart.php template:

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

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

But it is still not displayed.

Not sure what I'm doing wrong here.

Can anyone help with this?

thanks

+6
source share
3 answers

UPDATE COMPATIBILITY for WooCommerce version 3+

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

To get a description of the product description parameter in the basket (condition for the type of filtration product), you have 2 possibilities (maybe even more ...):

  • Display variation descriptions using woocommerce_cart_item_name , without editing any template.
  • Displays the description of the variation using the cart.php template.

In both cases, you do not need to use the foreach in the code, as mentioned earlier, because it already exists. Thus, the code will be more compact.

Case 1 - using woocommerce_cart_item_name :

 add_filter( 'woocommerce_cart_item_name', 'cart_variation_description', 20, 3); function cart_variation_description( $name, $cart_item, $cart_item_key ) { // Get the corresponding WC_Product $product_item = $cart_item['data']; if(!empty($product_item) && $product_item->is_type( 'variation' ) ) { // WC 3+ compatibility $descrition = version_compare( WC_VERSION, '3.0', '<' ) ? $product_item->get_variation_description() : $product_item->get_description(); $result = __( 'Description: ', 'woocommerce' ) . $descrition; return $name . '<br>' . $result; } else return $name; } 

In this case, the description is displayed only between the header values ​​and the attribute values.

This code is in the function.php file of your active child theme (or theme), as well as in any plug-in file.


Case 2 Using the cart/cart.php template ( Update as per your comment).

You can choose where you want to display this description (2 options):

  • After the name
  • After the header values ​​and variations.

Thus, you insert this code into the cart.php template around line 86 or 90 depending on your choice:

 // Get the WC_Product $product_item = $cart_item['data']; if( ! empty( $product_item ) && $product_item->is_type( 'variation' ) ) { // WC 3+ compatibility $description = version_compare( WC_VERSION, '3.0', '<' ) ? $product_item->get_variation_description() : $product_item->get_description(); if( ! empty( $description ) ) { // '<br>'. could be added optionally if needed echo __( 'Description: ', 'woocommerce' ) . $description;; } } 

All code is tested and fully functional.

+8
source

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; } 
+3
source

You can get it through the $woocommerce global variable as well -

 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.

0
source

All Articles