Posted on

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.4.7
 */

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, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

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

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

    Thanks Rodolfo

    1. Awesome ๐Ÿ™‚

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  16. 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're writing code, please wrap it between: [php] code_here [/php]

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.