特定产品类别的 WooCommerce 购物车商品价格后缀

时间:2021-04-28 19:11:01

标签: php wordpress woocommerce cart taxonomy-terms

我需要在 WooCommerce 购物车上为特定类别的 woocommerce 产品的价格添加后缀文本。

这是我的代码:

add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 ); 
add_filter( 'woocommerce_cart_item_subtotal', 'filter_woocommerce_cart_item_price', 10, 3 ); 
function filter_woocommerce_cart_item_price( $wc, $cart_item, $cart_item_key ) { 
    if(!in_array('67',$cart_item['data']->get_category_ids()))
    {
        return $wc;
    }
    else{
        return $wc . ' Monthly';
    }
}; 

问题是它只适用于简单的产品。似乎不会影响可变产品的价格(我在 3 个不同的网站上进行了测试)。

知道我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

要使其也适用于购物车中的产品变体项目,请改用以下内容:

add_filter( 'woocommerce_cart_item_price', 'cart_item_amounts_prefix', 10, 3 ); 
add_filter( 'woocommerce_cart_item_subtotal', 'cart_item_amounts_prefix', 10, 3 ); 
function cart_item_amounts_prefix( $amount, $cart_item, $cart_item_key ) { 
    if( has_term( array(67), 'product_cat', $cart_item['product_id'] ) ){
        $amount .= ' ' . __("Monthly", "woocommerce");
    }
    return $amount;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经测试有效。