[英]Allow only one product category in cart at once in Woocommerce
How would I go about configuring the Woocommerce cart to only allow one product category type in it at a time?我将如何 go 关于配置 Woocommerce 购物车一次只允许一种产品类别类型?
The following code will allow adding to cart only items from one product category avoiding add to cart and displaying a custom notice:以下代码将允许仅将来自一个产品类别的商品添加到购物车,从而避免添加到购物车并显示自定义通知:
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
function only_one_product_category_allowed( $passed, $product_id, $quantity) {
// Getting the product categories term slugs in an array for the current product
$term_slugs = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs') );
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
// Check if the product category of the current product don't match with a cart item
if( ! has_term( $term_slugs, 'product_cat', $cart_item['product_id'] ) ){
// Displaying a custom notice
wc_add_notice( __('Only items from one product category are allowed in cart'), 'error' );
// Avoid add to cart
return false; // exit
}
}
return $passed;
}
Code goes in function.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.
测试和工作。
Addition (updated) - The same but only for parent product categories :添加(更新) - 相同但仅适用于父产品类别:
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
function only_one_product_category_allowed( $passed, $product_id, $quantity) {
$parent_term_ids = $item_parent_term_ids = array(); // Initializing
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
}
}
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
// Loop through the cart item product category terms to get only parent main category term
foreach( get_the_terms( $cart_item['product_id'], 'product_cat' ) as $term ){
if( $term->parent > 0 ){
$item_parent_term_ids[] = $term->parent; // Set the parent product category
}
}
// Check if parent product categories don't match
if( ! array_intersect( $parent_term_ids, $item_parent_term_ids ) ){
// Displaying a custom notice
wc_add_notice( __('Only items from one product category are allowed in cart'), 'error' );
// Avoid add to cart
return false; // exit
}
}
return $passed;
}
Code goes in function.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.
测试和工作。
@LoicTheAztec Pls what if i want to allow just products within a certain category to be added to be added at checkout. @LoicTheAztec 如果我只想允许添加某个类别中的产品以在结帐时添加,该怎么办。 My case is, i have some bookable products and physical products and they both have different shipping fees.
我的情况是,我有一些可预订的产品和实物产品,它们都有不同的运费。 So i want a case where if someone orders a Bookable product they wont be able to add any physical product to cart unless they do that on a separate order.
所以我想要一个案例,如果有人订购可预订产品,他们将无法将任何实物产品添加到购物车,除非他们在单独的订单上这样做。 Thanks
谢谢
Thanks LoicTheAztec for providing the great answer!感谢LoicTheAztec提供了很好的答案!
However, the parent product categories version does not fit to my case.但是,父产品类别版本不适合我的情况。
LoicTheAztec 's version returns false when the products's category does not have parent, even those products have the same category. LoicTheAztec的版本在产品的类别没有父类别时返回 false,即使这些产品具有相同的类别。
Here's the modification of it to handle the above case.这是处理上述情况的修改。
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
function only_one_product_category_allowed( $passed, $product_id, $quantity) {
$parent_term_ids = $item_parent_term_ids = array(); // Initializing
// Loop through the current product category terms to get the parent main category term if any.
// Otherwise we get the current product's own category.
foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
if ( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
} else {
$parent_term_ids[] = $term->term_id; // Set the category itself of the current product for later comparison
}
}
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
// Loop through the cart item product category terms to get the parent main category term if any.
// Otherwise we get the cart item's own category.
foreach( get_the_terms( $cart_item['product_id'], 'product_cat' ) as $term ){
if ( $term->parent > 0 ){
$item_parent_term_ids[] = $term->parent; // Set the parent product category
} else {
$item_parent_term_ids[] = $term->term_id; // Set the category itself of the cart item for later comparison
}
}
// Check if parent product categories don't match
if( ! array_intersect( $parent_term_ids, $item_parent_term_ids ) ){
// Displaying a custom notice
wc_add_notice( __('Only items from one product category are allowed in cart'), 'error' );
// Avoid add to cart
return false; // exit
}
}
return $passed;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.