I have several categories and I’m trying to make it so that some categories cannot be added to the cart along with others. (when adding, there should be a redirect to the product and an error message is displayed).
I found the code that helped me here, but I couldn’t edit it correctly so that the rule would apply to other categories
The problem is this
The categories below can be added to the cart among themselves
cheese
cheese-sets
bread
bakery
milk
Whereas the following categories should not be mixed with the categories above and among themselves
glamp
horse
foodtruck
other
farm
original code from link
<?php
//*** Prevent mixture of paint and other prods in same cart ***//
function dont_add_paint_to_cart_containing_other($validation, $product_id) {
// Set flag false until we find a product in cat paint
$cart_has_paint = false;
// Set $cat_check true if a cart item is in paint cat
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
if (has_term('paint', 'product_cat', $product->id)) {
$cart_has_paint = true;
// break because we only need one "true" to matter here
break;
}
}
$product_is_paint = false;
if (has_term('paint', 'product_cat', $product_id)) {
$product_is_paint = true;
}
// Return true if cart empty
if (!WC()->cart->get_cart_contents_count() == 0) {
// If cart contains paint and product to be added is not paint, display error message and return false.
if ($cart_has_paint && !$product_is_paint) {
wc_add_notice('Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
$validation = false;
}
// If cart contains a product that is not paint and product to be added is paint, display error message and return false.
elseif (!$cart_has_paint && $product_is_paint) {
wc_add_notice('Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
$validation = false;
}
}
// Otherwise, return true.
return $validation;
}
add_filter('woocommerce_add_to_cart_validation', 'dont_add_paint_to_cart_containing_other', 10, 2);
?>
And reworked but not worked ^)
When "allowed" category in cart and i add any "prohibited" categoru its worked
But when i try add product from glamp and horse not worked? both product goes to cart andd rule not work
<?php
//*** Prevent mixture of certain categories in same cart ***//
function dont_add_categories_to_cart_containing_other($validation, $product_id) {
// Set flag false until we find a product in prohibited categories
$cart_has_prohibited = false;
// Prohibited categories
$prohibited_categories = array(
'glamp',
'horse',
'foodtruck',
'other',
'farm'
);
// Allowed categories
$allowed_categories = array(
'cheese',
'cheese-sets',
'bread',
'bakery',
'milk'
);
// Set $cat_check true if a cart item is in prohibited categories
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$product_categories = wp_get_post_terms($product->id, 'product_cat', array("fields" => "slugs"));
$intersect_categories = array_intersect($prohibited_categories, $product_categories);
if (!empty($intersect_categories)) {
$cart_has_prohibited = true;
// break because we only need one "true" to matter here
break;
}
}
$product_categories = wp_get_post_terms($product_id, 'product_cat', array("fields" => "slugs"));
$intersect_categories = array_intersect($prohibited_categories, $product_categories);
$product_is_prohibited = !empty($intersect_categories);
// Return true if cart empty
if (!WC()->cart->is_empty()) {
// If cart contains prohibited categories and product to be added is not prohibited, display error message and return false.
if ($cart_has_prohibited && !$product_is_prohibited) {
wc_add_notice('Sorry, you can only purchase products from the allowed categories. Please checkout your current cart or empty your cart and try again.', 'error');
$validation = false;
}
// If cart contains a product that is not prohibited and product to be added is from the prohibited categories, display error message and return false.
elseif (!$cart_has_prohibited && $product_is_prohibited) {
wc_add_notice('Sorry, you can only purchase products from the allowed categories. Please checkout your current cart or empty your cart and try again.', 'error');
$validation = false;
}
}
// Otherwise, return true.
return $validation;
}
add_filter('woocommerce_add_to_cart_validation', 'dont_add_categories_to_cart_containing_other', 10, 2);
?>
Help solve the problem
Thanks
2
Answers
Bhautik thank you for your help your questions and your help motivated me more than ever. I found another option that eventually works for me.
Based on the above discussion. I have added logic. try the below code.