We want the customers to only purchase the following quantities.
So, far; we've successfully managed to limit the number of quantities, that can be selected on the single product page,
We're currently using the following code to limit the user to a total of 10 Bags.
// Checking and validating when products are added to cart
add_filter( 'woocommerce_add_to_cart_validation', 'only_six_items_allowed_add_to_cart', 10, 3 );
function only_six_items_allowed_add_to_cart( $passed, $product_id, $quantity ) {
$cart_items_count = WC()->cart->get_cart_contents_count();
$total_count = $cart_items_count + $quantity;
if( $cart_items_count >= 10 || $total_count > 10 ){
// Set to false
$passed = false;
// Display a message
wc_add_notice( __( "Sorry, You can’t have more than 10 bags in your cart.", "woocommerce" ),
"error" );
}
return $passed;
}
Now, we just need to add a condition, which will limit the user (and display a warning), whenever they try to increase their cart quantity, between the range of 6-9 bags.
The problem is that, for example, a customer adds 5 bags to their cart and after that, what if they go back and try to add another bag?
I want to avoid this situation, so the customer is only able to purchase the above-mentioned quantities.
Please check this link to better understand the situation: https://mettaatta.com/product/metta-atta
This one :
if($cart_items_count >= 10 || $total_count > 10 ){
// Display a message
}
if($total_count >=6 && $total_count <=9){
// Display a message
}
Will work as you need.
EDIT
If you want to combine them :
if(($cart_items_count >= 10 || $total_count > 10) || ($total_count >=6 && $total_count <=9)){
// Display a message
}