If you want to display the short description in the cart and in the checkout you can do it using the woocommerce_cart_item_name
hook.
If you want to show the short description of all product types you can use this code:
// returns the short description as the cart item name
add_filter( 'woocommerce_cart_item_name', 'change_cart_item_name', 99, 3 );
function change_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
// get short description
$short_description = $product->get_short_description();
// if it exists, it returns the short description as the cart item name
if ( ! empty( $short_description ) ) {
return $short_description;
} else {
return $item_name;
}
return $item_name;
}
If, in the case of product variations, you want to show the short description of the parent product (variable product) you can use the following code:
// returns the short description as the cart item name
add_filter( 'woocommerce_cart_item_name', 'change_cart_item_name', 99, 3 );
function change_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
// if it is a variation it gets the description of the variable product
if ( $product->is_type( 'variation' ) ) {
$parent_id = $product->get_parent_id();
$variable = wc_get_product( $parent_id );
$short_description = $variable->get_short_description();
} else {
// get short description
$short_description = $product->get_short_description();
}
// if it exists, it returns the short description as the cart item name
if ( ! empty( $short_description ) ) {
return $short_description;
} else {
return $item_name;
}
return $item_name;
}
It works both on the cart page and in the checkout.
The code must be added in your theme's functions.php file.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…