【问题标题】:Display WooCommerce cart item short description for a specific product categories显示特定产品类别的 WooCommerce 购物车项目简短描述
【发布时间】:2017-01-26 21:11:21
【问题描述】:

在 WooCommerce 中,我正在尝试为购物车项目中的特定类别添加产品简短描述。

我发现 this code 将产品简短描述添加到购物车中的所有产品,但我不知道如何将其缩小到仅显示在特定产品上:

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

如何使此代码仅显示已定义产品类别的简短描述?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce checkout categories


    【解决方案1】:

    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 文件或任何插件文件中。

    代码已经过测试并且可以工作。

    【讨论】:

    • 感谢 Loic,感谢您在这方面的帮助!
    猜你喜欢
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2018-12-10
    相关资源
    最近更新 更多