[英]Display WooCommerce cart item short description for a specific product categories
In WooCommerce I'm trying to add the product short description for a specific category in the cart items.在 WooCommerce 中,我试图为购物车项目中的特定类别添加产品简短描述。
I found this code that adds the product short description to ALL products in the cart, but I can't figure out how to narrow it down to only display on specific products:我发现此代码将产品简短描述添加到购物车中的所有产品,但我不知道如何将其缩小到仅显示在特定产品上:
add_filter('woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2);
function wc_checkout_description_so_27900033($other_data, $cart_item) {
$post_data = get_post($cart_item['product_id']);
echo $post_data - > post_excerpt;
return $other_data;
}
How can I make this code display the short description only for a defined product category?如何使此代码仅显示已定义产品类别的简短描述?
Thanks谢谢
Update for woocommerce versions 3 and above woocommerce 版本 3 及更高版本的更新
I have changed and actualized a little bit your code.我已经改变并实现了一点你的代码。 Then to target a product category you should use
has_term()
conditional WordPress function.然后要定位产品类别,您应该使用
has_term()
条件 WordPress 函数。
You will have to define in the function your categories IDs, slugs or names.您必须在函数中定义类别 ID、slug 或名称。
So here is the code for defined product categories terms:所以这里是定义的产品类别术语的代码:
add_filter('woocommerce_get_item_data', 'filter_woocommerce_get_item_data', 10, 2);
function filter_woocommerce_get_item_data( $item_data, $cart_item ) {
// Define HERE your Category term IDs, Slugs or Names in the array
$categories = array('clothing', 'music');
// Product Category condition Below
if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
if( ! ( $cart_item['variationt_id'] > 0 ) ) {
$description = $cart_item['data']->get_short_description();
} else {
$description = $cart_item['data']->get_description();
if ( ! empty( $description ) ) {
$parent_product = wc_get_product( $cart_item['product_id'] );
$description = $parent_product->get_short_description();
}
}
if ( ! empty( $description ) ) {
$item_data[] = array(
'key' => __( 'Product description', 'woocommerce' ),
'value' => $description,
'display' => $description,
);
}
}
return $item_data;
}
Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.代码位于您的活动子主题(或主题)的 functions.php 文件中或任何插件文件中。
Code is tested and works.代码经过测试并有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.