在Wooccommerce 3中隐藏特定产品类别的购物车优惠券字段


function hidding_coupon_field_on_cart_for_a_category($enabled) {
// Set your special category name, slug or ID here:
$special_cat = 'clothing';
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$wc_product = $cart_item['data'];
// Woocommerce compatibility
$product_id = method_exists( $wc_product, 'get_id' ) ? $wc_product->get_id() : $wc_product->id;
if ( has_term( $special_cat, 'product_cat', $product_id ) )
$bool = true;
}
if ( $bool && is_cart() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hidding_coupon_field_on_cart_for_a_category' );

找到了这个片段,效果很好,但如果我有一个变量而不是一个简单的产品,那么隐藏类别似乎就不起作用了。

$special_cat = 'prints';
$product_id  = array('9461', '9597');

此外,排除主产品id和变体id也不起作用。有人有主意吗?

更新2-2018年10月(仍然可以完美地使用Wooccommerce的上一个版本(

要使其适用于所有产品类型,包括可变产品,请使用以下内容:

add_filter( 'woocommerce_coupons_enabled', 'product_category_hide_cart_coupon_field', 20, 1 );
function product_category_hide_cart_coupon_field( $enabled ) {
// Only on frontend
if( is_admin() ) 
return $enabled;
// Set your special categories names, slugs or IDs here in the array:
$categories = array('prints');
$found = false; // Initializing
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
$found = true;
break; // We stop the loop
}
}
if ( $found && is_cart() ) {
$enabled = false;
}
return $enabled;
}

代码位于活动子主题(或活动主题(的function.php文件中。测试并工作。

相关内容

最新更新