显示特定产品类别的WooCommerce购物车项目简短描述 [英] Display WooCommerce cart item short description for a specific product categories

查看:61
本文介绍了显示特定产品类别的WooCommerce购物车项目简短描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我试图在购物车项目中添加特定类别的产品简短说明.

In WooCommerce I'm trying to add the product short description for a specific category in the cart items.

我发现> 此代码 ,它将产品简短说明添加到购物车中的所有产品,但我不知道如何缩小范围以仅显示在特定产品上:

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?

谢谢

推荐答案

woocommerce版本3及更高版本的更新

Update for woocommerce versions 3 and above

我已经更改并实现了一些代码.然后,要定位产品类别,您应该使用 has_term() 有条件的WordPress功能.

I have changed and actualized a little bit your code. Then to target a product category you should use has_term() conditional WordPress function.

您将必须在函数中定义类别ID,段或名称.

You will have to define in the function your categories IDs, slugs or names.

这是定义的产品类别术语的代码:

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;
}

代码会出现在您活动的子主题(或主题)的functions.php文件或任何插件文件中.

代码已经过测试并且可以正常工作.

Code is tested and works.

这篇关于显示特定产品类别的WooCommerce购物车项目简短描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆