[英]I want to limit it to add only products from one category and not allow others with woocommerce
I'm trying to limit it, so I can only add products with the same category and not allow others, returning a message, but when I try to add another product from the same category, it returns the defined error message.我试图限制它,所以我只能添加具有相同类别的产品而不允许其他产品,返回一条消息,但是当我尝试添加来自同一类别的另一个产品时,它返回定义的错误消息。 My code:
我的代码:
// define your product category (can be term IDs, slugs or names)
$category = 'courses';
// close if cart is empty
if( WC()->cart->is_empty() )
return $passed;
// check cart: search by course category items
foreach ( WC()->cart->get_cart() as $cart_item ){
if( has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
// display a warning message
wc_add_notice( __('Not possible to add courses together with products.', 'woocommerce' ), 'error' );
// avoid adding to cart
return false;
}
}
return $passed;
}
I believe you only need to fix this line by adding "!"我相信你只需要通过添加“!”来修复这一行。 as you're looking to deny different categories:
因为你想拒绝不同的类别:
if ( ! has_term( $category, 'product_cat', $cart_item['product_id'] ) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.