WooCommerce: Display also product variation description on cart items
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.
Display product variation description or product short description based on product type in WooCommerce checkout
To display product variation description instead of product short description for variable products, you can use:
// Display on cart & checkout pages
function filter_woocommerce_get_item_data( $item_data, $cart_item ) {
// Compare
if ( $cart_item['data']->get_type() == 'variation' ) {
// Get the variable product description
$description = $cart_item['data']->get_description();
} else {
// Get product excerpt
$description = get_the_excerpt( $cart_item['product_id'] );
}
// Isset & NOT empty
if ( isset ( $description ) && ! empty( $description ) ) {
$item_data[] = array(
'key' => __( 'Description', 'woocommerce' ),
'value' => $description,
'display' => $description,
);
}
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'filter_woocommerce_get_item_data', 10, 2 );
Display product (variation) description in a new line on WooCommerce order received page and emails
To display the $decription
on a new line, you can use the woocommerce_order_item_meta_start
action hook versus woocommerce_order_item_name
filter hook.
So you get:
function action_woocommerce_order_item_meta_start( $item_id, $item, $order, $plain_text ) {
// Get product
$product = $item->get_product();
// Variation description
if ( $item['variation_id'] > 0 ) {
$description = $product->get_description();
} else {
// Product short description (for others)
$description = $product->get_short_description();
}
echo '<div>' . $description . '</div>';
}
add_action( 'woocommerce_order_item_meta_start', 'action_woocommerce_order_item_meta_start', 10, 4 );
Woocommerce: Display variation name in cart, order confirmation, and order email
Through the article here, I found I was able to achieve what I wanted with a very simple filter:
add_filter('woocommerce_add_cart_item_data','my_add_item_data',10,3);
function my_add_item_data($cart_item_data, $product_id, $variation_id)
{
if(isset($_REQUEST['size']))
{
$cart_item_data['size'] = sanitize_text_field($_REQUEST['size']);
}
return $cart_item_data;
}
This has added the variation info to cart, checkout confirmation, and the email. So simple!
Related Topics
Destroy PHP Session on Page Leaving
PHP Static Method Call with Variable Class Name and Namespaces
Add Custom Columns to Admin Products List in Woocommerce 3
How to Send a File via Axios to Laravel
How to Display Images from a Folder Using PHP - PHP
Customize Tax Amount in "Woocommerce_Package_Rates" Hook
Php: Re Order Associative Array
PHP Thumbnail Image Resizing with Proportions
How to Remove Values from an Array in PHP
Replace the Last Comma in a String Using Regular Expression
Elegant Way to Search an PHP Array Using a User-Defined Function
How to Curry Method Calls in PHP
Output (Echo/Print) Everything from a PHP Array
Seems Like Post Values Are Lost When .Htaccess Rewriterule Used. Get Values Are Ok. How to Fix