问题
I'm struggling to find a simple solution to this problem I'm having with One-page-checkout plugin for Woocommerce.
My client would like to add the product description next to the product title in cart items.
Any thoughts on how I can manipulate the code to show the description?
This is what I have actually:
I would think that this plugin would just be hiding the description somewhere but I can't locate it anywhere in the code.
回答1:
There is 2 ways to do it (making work for products and product variations):
1) With custom function hooked in woocommerce_get_item_data
action hook (The best way):
add_filter( 'woocommerce_get_item_data', 'customizing_cart_item_data', 10, 2 );
function customizing_cart_item_data( $cart_data, $cart_item ) {
$custom_items = array();
$label = __( 'Description', 'woocommerce' );
// Get the product description
$description = $cart_item['data']->get_description();
// For product variations when description is empty
if( $cart_item['data']->is_type('variation') && empty( $description ) ){
// Get the parent variable product object
$product = wc_get_product( $cart_item['data']->get_parent_id() );
// Get the variable product description
$description = $product->get_description();
}
// If product or variation description exists we display it
if( ! empty( $description ) ){
$custom_items[] = array(
'key' => $label,
'display' => $description,
);
}
// Merging description and product variation attributes + values
if( ! empty( $cart_data ) ) $custom_items = array_merge( $custom_items, $cart_data );
return $custom_items;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
… or …
2) With custom function hooked in woocommerce_cart_item_name
filter hook:
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_data', 10, 3);
function customizing_cart_item_data( $item_name, $cart_item, $cart_item_key ) {
// The label
$label = __( 'Description', 'woocommerce' );
// Get the product description
$description = $cart_item['data']->get_description();
// For product variations when description is empty
if( $cart_item['data']->is_type('variation') && empty( $description ) ){
// Get the parent variable product object
$product = wc_get_product( $cart_item['data']->get_parent_id() );
// Get the variable product description
$description = $product->get_description();
}
if( ! empty( $description ) ){
$item_name .= '<p class="item-description" style="margin:12px 0 0;">
<strong>'.$label.'</strong>: <br>'.$description.'
</p>';
}
return $item_name;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested on Woocommerce 3+ and works.
回答2:
You could try this code in your functions.php located in your root of your theme folder. Not sure if it still works as I'm not active in Wordpress development anymore. [untested]
Updated: this should probably work for WooCommerce 3+
Using woocommerce_cart_item_name hook:
add_filter( 'woocommerce_cart_item_name', 'cart_description', 20, 3);
function cart_description( $name, $cart_item, $cart_item_key ) {
// Get the corresponding WC_Product
$product_item = $cart_item['data'];
if(!empty($product_item)) {
// WC 3+ compatibility
$description = $product_item->get_description();
$result = __( 'Description: ', 'woocommerce' ) . $description;
return $name . '<br>' . $result;
} else
return $name;
}
}
来源:https://stackoverflow.com/questions/46732243/add-product-description-to-cart-items-in-woocommerce