Yes, there are many plugins that already achieve this. But my goal at Business Bloomer is to save you from plugin conflicts, delicate updates and to make you learn some PHP.
So, here’s how you can add, with a few lines of PHP, a minimum, maximum, increment and default value to your Add to Cart quantity input field on the single product and cart pages. Who knew it was this easy?
- PHP Snippet 1: Set Min, Max, Increment & Default Value Add to Cart Quantity @ WooCommerce Single Product Page & Cart Page (Simple Products)
- PHP Snippet 2: Set Min Add to Cart Quantity @ WooCommerce Single Product Page (Variable Products -> Single Variation)
- Advanced Plugin 1: WooCommerce Quantity Discounts, Rules & Swatches
- Advanced Plugin 2: WooCommerce Quantity Manager
PHP Snippet 1: Set Min, Max, Increment & Default Value Add to Cart Quantity @ WooCommerce Single Product Page & Cart Page (Simple Products)
/**
* @snippet Min, Max, Increment & Default Value Quantity | WooCommerce
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 8
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_quantity_input_args', 'bloomer_woocommerce_quantity_changes', 9999, 2 );
function bloomer_woocommerce_quantity_changes( $args, $product ) {
if ( ! is_cart() ) {
$args['input_value'] = 4; // Start from this value (default = 1)
$args['max_value'] = 10; // Max quantity (default = -1)
$args['min_value'] = 4; // Min quantity (default = 0)
$args['step'] = 2; // Increment/decrement by this value (default = 1)
} else {
$args['max_value'] = 10; // Max quantity (default = -1)
$args['step'] = 2; // Increment/decrement by this value (default = 0)
// COMMENT OUT FOLLOWING IF STEP < MIN_VALUE
// $args['min_value'] = 4; // Min quantity (default = 0)
}
return $args;
}
PHP Snippet 2: Set Min Add to Cart Quantity @ WooCommerce Single Product Page (Variable Products -> Single Variation)
/**
* @snippet Min Add to Cart Quantity for Variations | WooCommerce
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 8
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_available_variation', 'bloomer_woocommerce_quantity_min_variation', 9999, 3 );
function bloomer_woocommerce_quantity_min_variation( $args, $product, $variation ) {
$args['min_qty'] = 5;
return $args;
}
Advanced Plugin 1: WooCommerce Quantity Discounts, Rules & Swatches
If youβd love to code but donβt feel 100% confident with that, there are plugin alternatives.
Based on reputation, support quality, code cleanliness, long-term reliability, theΒ WooCommerce Quantity Discounts, Rules & Swatches pluginΒ by Studio Wombat is probably your best bet.
With an easy to understand settings panel you can enable tiered pricing,Β quantity swatches, min & max quantities, tooltips and multilingual support all from the same settings panel.
Advanced Plugin 2: WooCommerce Quantity Manager
In case you want another code-free solution, WooCommerce Quantity Manager may suit your needs as well.
This well-coded and fully-featured plugin has a full range of quantity management features. You can set the default quantity value to anything you like, including zero. There are also options to set quantity minimums, maximums, and step values.
Hi Rodolfo
The code snippet is working, but for the variations where I altered the min amount and incremental steps to 0.01, the add to cart is not working anymore. Any idea why?
If I recall correctly, the default add to cart amount for WooCommerce is 1? Do I need to change that as well in order to make this work?
Regards
Pieter
Hi Pieter, I believe variations require an additional snippet: https://stackoverflow.com/questions/64861903/enable-decimal-quantities-and-stock-for-woocommerce-products
Dont work on products Home front page —
Hi Anderson, what do you use to display products on the home page?
Hi
thanks for this great code.
but this code dosent work for woocommerce add_to_cart ajax button.
do you have any solution for this problem.
Hi Sajad, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hey =)
I would like to use a direct Add-to-Cart link on my salespage to skip the shopping cart. I am using the following link: https://www.mydomain.com/checkout/?add-to-cart=19 (product ID is 19).
It works fine but the problem is that if the buyer clicks on this link several times, for every click another product gets added in the checkout. For example if the buyer clicks on the link twice the quantity on the checkout page will be 2.
Is it possible to restrict this so that even when the buyer clicks several times on the link the quantity will be 1?
But the buyer should still be able to adjust the quantity on the checkout page manually.
You could disable click after the first click (jQuery)
Helloo, Thank you Rodolfo for the awesome post. it works but i have an issue. the – and + button only increment or reduce by one then doesn’t work. but if i type the number myself i can add it to card. would this be a theme issue ? they’re not answering me. if you have any idea where i could fix that it would be amaazing. Thank you
Default Woo has no + – buttons
Hi Rodolfo,
Thanks for your awesome code snippets!
I implemented the code exactly as per snippet 1 and have this issue:
Single product page
The + and – buttons stop at the min and max value and step correctly. However, the user can still just type a qty and press ‘Add to cart’. No validation warnings show and the user can enter any value included outside the min and max range. The qty is added to the cart and the user can checkout – eg selecting 3 units.
Cart page
Validations show when the user types the qty and presses Update and has entered an invalid option.
Should the validations also work on the single product page?
Hi Nick, I get validation errors when I try to add to cart from the single product page:
Hello
Can we add min order quantity according to the country region?
Thanks
Akshay, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
When I select swatched options it returns to 1 and goes up by the step I set up in this code. have you checked that?
Not sure I fully understand
Hi,
thanks for your blog, source of inspiration!
I created this snippet, it works well in the product pages but not in the cart, what could I change?
thanks a lot
Hi Pasquale, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
How can i change the steps in the cart only for a specific item in the category downloads. So when i have more products and one in the category Downloads. How can I change the steps quantity only for that product?
Winsley, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi Rodolfo,
thank you so much for this snippet.
Is it posible to exclude single products?
Hi Uli, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Actually, it works.
Thanks
Thank you!
Hi there, i think your job it’s amazing and i wonder if u can help me.
I added the quantity buttons in the loop page (and I made, work 100%), now I want to restrict the numbers and the step it’s not possible for me because instead of 1-2-3-4-5-6-7-8-9 i want to do 1-2-3-4-6-12-18-24
Thanks
Hello Wilson, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
I need to set the default quantity box add to cart number to 0, is it possible?
The quantity selector shows 1, and people won’t add to cart the tickets.
We need the opc quantity selector to be set with 0, so that the customers understand that they have to select the quantity and click ADD TICKETS (in spanish, “aΓ±adir localidades”).
Is it possible to set 0 as default selection in the quantity box?
Hello Mario, thanks so much for your comment! Did you try setting the min to 0? Anyway, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
Hi, how do I add a variable?
$args[‘min_value’] = the_field(‘minimum_order’); ???????
Hey Alex, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hi there!
Thanks for this great tutorial!
Quick question β is it possible to only target grouped products? In my WooCommerce store simple products already have their default quantity values set to ‘1’, but I’d like it to be ‘1’ as well for grouped products and also make it a minimum of ‘1’ for each product within the grouped product. Hope that makes sense π
Thanks!
Hello Jonas, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hi,
I want t set this up for individual products, and have them start at 5 or 2.
However, users are able to hit the minus – and go down from 5 to 1 so there is no set MIN quantity it only starts at that number.
Cheers,
Hello Matt, thanks for your comment! Not sure I fully understand, but either way this snippet still works, I just tested it.
Hi, I want this function for my specific product id. Please help me how can I use this function for specific product only.
Hey Anas, thanks for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know π
Works like a charm π
wp 4.9.8
storefront 2.3.5
Thanks Rodolfo
Awesome π
Hello Rodolfo.
Is there any way to leave minimum and maximum value undefined? My eshop has different min. values for every product. I would like to set just step value.
Thanks.
xtrmntr
Hey there, thanks for your comment! Have you tried using the following:
Hi Rodolfo,
the code works fine when adding to cart, but once on the cart one can easily change the quantity and set them lower than required. That is due to the !is_cart(), I imagine. If I remove that condition then strange things happen on the cart, such as the quantity going back to minimum after recalculating (but the total would be correct). E.g.:
If minimun is 10 and the price is 1$ I could set it to Q=20 and after recalculating it would show a total of 20$, but Q back to 10, which is very confusing for the visitor.
How to solve it, please?
Hello Franco, I just tested the code and it works wonderfully on both Cart and Single Product. And also when you “update Cart”. So please try using a different theme for testing e.g. Storefront or 2017, and deactivate all plugins.
The only thing is this – if “min” is greater than “step” (e.g. step = 2 and min = 4) everything works on the single product page but you can indeed select a lower quantity on the Cart (2 in this case), which is not acceptable. However, the Cart item minimum quantity must be 0, so this creates a little problem. Let me look into it… I’ll reply here soon
Snippet updated with an extra line in the Cart section. You won’t be able to set quantity = 0 in the Cart, but you still have the “Remove Item” button. Hope this helps!
Oh yes, it works. I had made some mistakes due to some added complexity in order to set different min values for specific products. Great work π
π
Thank you so much!!! Incremental quantities plugin had conflicts with all the “Hide the price until login” plugins i could find and your solution works with them! Big help!
Great π
Hi
I have a probleme with this code, it put the default quantiti also to the cart (warenkorb). i have definde as default 6, and i i change it to 5 it shows in the cart 6 as quantiti, but the total value is correct. so the user has change ist again.
WP 4.9.1
Woocommerce 3.2.6
Hi, i found a better solution on docs.woocommerce.com
// Simple products
add_filter( ‘woocommerce_quantity_input_args’, ‘jk_woocommerce_quantity_input_args’, 10, 2 );
function jk_woocommerce_quantity_input_args( $args, $product ) {
if ( is_singular( ‘product’ ) ) {
$args[‘input_value’] = 2; // Starting value (we only want to affect product pages, not cart)
}
$args[‘max_value’] = 80; // Maximum value
$args[‘min_value’] = 2; // Minimum value
$args[‘step’] = 2; // Quantity steps
return $args;
}
// Variations
add_filter( ‘woocommerce_available_variation’, ‘jk_woocommerce_available_variation’ );
function jk_woocommerce_available_variation( $args ) {
$args[‘max_qty’] = 80; // Maximum value (variations)
$args[‘min_qty’] = 2; // Minimum value (variations)
return $args;
}
Thanks so much π
Rodolfo, thank you for this helpful snippet! How would I specify a maximum quantity (for one particular product in the store) based on cart total, e.g. one allowed per $10 in cart?
Dan, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hi Rodolfo,
A really useful add-on that set me to thinking … my website will have 2 user roles – retail and wholesale. I would like to have the unit of sale for a retail user as 1, and the unit of sale for a wholesale user as 6. Could a condition be added to this snippet such that it would only work for a wholesale user. The increment would be 6 if a wholesale user was logged in but only 1 if not?
Many thanks
Hello there, thanks so much for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know π
Hey Rodolfo, I implemented the snippet, using input value = 100, and step = 100.
It starts fine, but when I click on the quantity increment button (I’m testing in Chrome), it increments like this: 100 > 101 > 201 > 301 > …
Then when I decrement: 301 > 201 > 101 > 1
It should be 100 > 200 > 300… / 300 > 200 > 100 > 0
Anyone else with this phenomenon?
Thank You so much!
Pretty weird Peter π Can you try with a different theme and no extra plugins?
I am having exactly the same issue…..? Was there ever a solution posted?
Such a sweet piece of code! just climbs in increments of what I set +1
e.g. 1000 is 1001 and not 100 like specified
Dear Rodolfo,
I tried your snippet and it works great on product page.
If I visit cart page total value is fine, but qty. didn’t changed there and it starts with my input value 100:
$args['input_value'] = 100;
It would be nice if somebody else can confirm this bug.
Also it would be good to expand this snippet so that we can target products in categories.
Great work. Keep going and cheers ! π
Hey Danijel, thanks for your comment! Could you try with a different theme and no plugins please?
I use enfold and there was only woocommerce installed. However I solve this with the similar snippet I found on Woocommerce website. I just change “product_cat” into “product_tag”. Both solutions work fine, but in my situation version with “product_tag” is better, because I don’t want to use more then 1 category per product, and I need those categories for SEO.
function jk_woocommerce_quantity_input_args( $args, $product ) {
if (( has_term('10', 'product_tag', $product->post ) ) ) {
$args['step'] = 10;
$args['min_value'] = 10;
return $args;
}
else if (( has_term('100', 'product_tag', $product->post ) ) ) {
$args['step'] = 100;
$args['min_value'] = 100;
return $args;
}
else {
$args['step'] = 1;
$args['min_value'] = 1;
return $args;
}
}
Ok π
Hi Rodolfo, this updates the quantity but doesn’t correspondingly update the price
Jonathan, thanks for your comment! Are you 100% sure? Can you try with another theme for a moment?
Hi Rodolfo,
I got the same as Nicolas, the min works fine but the max limit can be trespassed. Using WP 4.8 and woocommerce 3.1.0. Any suggestion?
Regards
I’ve tried this on WP 4.8, Woo 3.1.0 – on the single product page the “plus minus” is indeed blocked at the value I set in the snippet. what does not work is if you add to cart the max, and then go back to the product and add to cart again. It will add more quantity and not restrict that. Is this the problem?
Hello Rodolpho !
The site and your teaching are excellent in general ! Keep going !
About this snippet, it works for me, except for the max quantity…. i can still select + 10 items and add them to the cart.
Woocommerce version: 2.6.14 (maybe it will works on the updated woocommerce ?)
Hey Nicolas, thanks for your comment! Yes, please, test it on the latest version and let me know π
Rodolfo, thank you for sharing this snippet!
I’ve tried it and it does a great job, but unfortunately it changes *all* the products in my store. Is there a way to customize it so it only affects specific products?
Thank you for all the help!
Hey John, thanks for your comment! See comment https://businessbloomer.com/woocommerce-define-add-cart-min-max-incremental-quantities/#comment-24572 π
It is a cool snippet, but actually doesnt work.
Melanie, thanks for your comment! Are you use this doesn’t work? It worked perfectly during my test – see blog post image π Let me know
Hi there, great snippet!
But is there a way to make this effective only for a specific product or category (since is_product / is_category conditions cannot be run into functions.php?
Tnx a lot!
Thank you Alessandro. Yes this is absolutely possible, and you can run is_product() in functions.php, not sure where you got that from π I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know π
Hi,
With the Add Min, Max, Increment & Start Value @ Add to Cart Quantity | WooCommerce Single Product Page, is it possible to do this for different products. I have several different products, all of which have different quantity minimums, maximums and steps.
Thank You
Hey Haris, thanks for your comment! Yes, this is possible of course. I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know π
Hello! I’m very new to this, so please have mercy π
But I read through so many search results now and tried so much to get it to work, but just can’t.
I want to limit the restricted quantities just to some products (those with a certain tag). So the closest (I think) I came was this:
Unfortunatly it’s not working π
Pleast help me out! Thanks!
Hey David, according to https://businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/, you can get the product tag IDs with:
After that, with a PHP foreach you can loop through the IDs to see if your tag ID is in there π
Hope this helps!