[英]Display WooCommerce cart item short description for a specific product categories
在 WooCommerce 中,我试图为购物车项目中的特定类别添加产品简短描述。
我发现此代码将产品简短描述添加到购物车中的所有产品,但我不知道如何将其缩小到仅显示在特定产品上:
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;
}
如何使此代码仅显示已定义产品类别的简短描述?
谢谢
woocommerce 版本 3 及更高版本的更新
我已经改变并实现了一点你的代码。 然后要定位产品类别,您应该使用has_term()
条件 WordPress 函数。
您必须在函数中定义类别 ID、slug 或名称。
所以这里是定义的产品类别术语的代码:
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;
}
代码位于您的活动子主题(或主题)的 functions.php 文件中或任何插件文件中。
代码经过测试并有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.