Woocommerce中特定产品类别的空购物车项目价格 [英] Null cart item price for a specific product category in Woocommerce
问题描述
我想从购物车总计中删除/取出特定类别产品的价格。
I want to remove / takeout the price of a specific category products from Cart Total.
请查看此屏幕截图:
我可以用什么钩子做到这一点?
What hook could I use to do this?
谢谢。
推荐答案
正确的选择是使用 has_term()
woocommerce_before_calculate_totals
/ code> WordPress条件函数可以过滤购物车商品中的产品类别。这样,您可以将购物车中的商品价格设为空。
The right hook to do that is woocommerce_before_calculate_totals
using has_term()
WordPress conditional function to filter product categories in cart items. This way you can null the price for that cart items.
这是代码(增加了对Woocommerce 3+的兼容性):
add_action( 'woocommerce_before_calculate_totals', 'custom_price_product_category', 20, 1 );
function custom_price_product_category( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
// When a product has 'glass' as category we null the price.
if( has_term( 'glass', 'product_cat', $cart_item["product_id"] ) ){
// Added Woocommerce 3+ compatibility
if( version_compare( WC_VERSION, '3.0', '<' ) )
$item['data']->price = '0';
else
$item['data']->set_price(0);
}
}
}
这在function.php中您的活动子主题(或主题)的文件,或者也包含在任何插件文件中。
This goes in function.php file of your active child theme (or theme) or also in any plugin file.
此代码已经过测试并且可以正常工作。
This code is tested and works.
这篇关于Woocommerce中特定产品类别的空购物车项目价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!