I want to restrict add to cart button with a max quantity of 10 per product. I want customers cannot purchase more than 10 quantity per product per order.
Here is the code
add_filter( 'woocommerce_add_to_cart_validation', 'restrict_per_product_quantity' );
function restrict_per_product_quantity($cart_item_data) {
global $woocommerce;
$item_count = $woocommerce->cart->cart_contents_count;
if($item_count > 10) {
wc_add_notice( 'Sorry, Only 10 quantity per product per order is allowed. If you would like to order more please contact support.', 'error' );
return false;
}
return $cart_item_data;
}
The issue is this code did not work with per product, it checks the cart, and if there are 10 items in the cart it displays the error.
2
Answers
woocommerce_add_to_cart_validation
contains 5 parameters that you can useUse these hooks to set minimum and maximum Quantity for product.