WooCommerce: Define Add to Cart Min, Max & Step Quantity

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 starting value to your Add to Cart quantities. Who knew it was this easy?

WooCommerce Min/Max, Increment and Start Value Add to Cart Quantities

PHP Snippet: Add Min, Max, Increment & Start Value @ Add to Cart Quantity | WooCommerce Single Product Page


/**
 * @snippet       Min, Max, Increment & Start Value Add to Cart Quantity | WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=72519
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'woocommerce_quantity_input_args', 'bloomer_woocommerce_quantity_changes', 10, 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 {

      // Cart's "min_value" is already 0 and we don't need "input_value"
      $args['max_value'] = 10; // Max quantity (default = -1)
      $args['step'] = 2; // Increment/decrement by this value (default = 0)
      // ONLY ADD FOLLOWING IF STEP < MIN_VALUE
      $args['min_value'] = 4; // Min quantity (default = 0)

   }
 
   return $args;
 
}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

52 thoughts on “WooCommerce: Define Add to Cart Min, Max & Step Quantity

  1. 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?

    1. 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

  2. Hi, how do I add a variable?
    $args[‘min_value’] = the_field(‘minimum_order’); ???????

    1. 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

  3. 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!

    1. 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

  4. 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,

    1. Hello Matt, thanks for your comment! Not sure I fully understand, but either way this snippet still works, I just tested it.

  5. Hi, I want this function for my specific product id. Please help me how can I use this function for specific product only.

    1. 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 ๐Ÿ™‚

  6. Works like a charm ๐Ÿ™‚
    wp 4.9.8
    storefront 2.3.5

    Thanks Rodolfo

    1. Awesome ๐Ÿ™‚

  7. 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

    1. Hey there, thanks for your comment! Have you tried using the following:

       = ''; 
  8. 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?

    1. 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

    2. 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!

    3. 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 ๐Ÿ™‚

    4. ๐Ÿ™‚

  9. 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!

    1. Great ๐Ÿ™‚

  10. 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

    1. 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;
      }

    2. Thanks so much ๐Ÿ™‚

  11. 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?

    1. 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

  12. 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

    1. 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 ๐Ÿ™‚

  13. 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!

    1. Pretty weird Peter ๐Ÿ™‚ Can you try with a different theme and no extra plugins?

    2. 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

  14. 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 ! ๐Ÿ™‚

    1. Hey Danijel, thanks for your comment! Could you try with a different theme and no plugins please?

    2. 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;
      }
      }

  15. Hi Rodolfo, this updates the quantity but doesn’t correspondingly update the price

    1. Jonathan, thanks for your comment! Are you 100% sure? Can you try with another theme for a moment?

  16. 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

    1. 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?

  17. 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 ?)

    1. Hey Nicolas, thanks for your comment! Yes, please, test it on the latest version and let me know ๐Ÿ™‚

  18. 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!

  19. It is a cool snippet, but actually doesnt work.

    1. 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

  20. 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!

    1. 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 ๐Ÿ™‚

  21. 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

    1. 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 ๐Ÿ™‚

    2. 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:

      add_filter( 'woocommerce_quantity_input_args', 'bloomer_woocommerce_quantity_changes', 10, 2 );
        
      function bloomer_woocommerce_quantity_changes( $args, $product ) {
        
      if( $product->is_product_tag( 'example' )) {
         $args['input_value'] = 5;
         $args['max_value'] = 100; 
         $args['min_value'] = 3; 
         $args['step'] = 1;
        }
      
         return $args;
       
      }
      

      Unfortunatly it’s not working ๐Ÿ™

      Pleast help me out! Thanks!

    3. 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:

      $product->get_tag_ids();
      

      After that, with a PHP foreach you can loop through the IDs to see if your tag ID is in there ๐Ÿ™‚

      Hope this helps!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.