Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to set minimum and maximum allowable product quantities to be added in WooCommerce Cart

Are you planning to define the minimum and maximum quantities to the products which you are selling on your store build up with WooCommerce? If yes, then you can achieve your goal simply by using filters available in WooCommerce OR by using some plugins available on WordPress Repository.

First, we will see how to limit the minimum and maximum quantity of products that can be added to WooCommerce cart using the filters in WooCommerce. Below are the two filters which allow you to change the default min and max values for the quantity selector field.

  • woocommerce_quantity_input_min : Allows to change the minimum value for the quantity. Default value is 0.
  • woocommerce_quantity_input_max : Allows to change maximum value for the quantity. Default value is -1.

Below are mainly 3 ways to apply the custom code on WordPress website.
1. Use the Code Snippets Plugin
2. Use a Custom Plugin
3. Use Your Child Theme functions.php

The easiest way I prefer among these 3 is to place the custom code in functions.php of the theme.

So paste the above code snippet in your currently active theme’s functions.php file and visit the front end of the WooCommerce product page. You will be able to select quantity only between 2 to 5 for that item.

Minimum and Maximum allowed quantity - set minimum and maximum quantities in WooCommerce
Minimum and Maximum allowed quantity

The above solution will only work on the WooCommerce product page. Which means, even after placing above code snippet in functions.php file, the customer can update the quantity on Cart page. Also, it will be applied to all the product available on your store.

To correct it, this will need some validations and some options where store owner can set the values to minimum and maximum quantity for the product.

So let’s create these options on the Product Meta box so that you can set this quantity for each product separately. And based on the values set to these options we can perform validations on the Cart page.

Creation of Minimum & Maximum Quantity fields:

Inventory tab of Product data meta box will be the best place to add our options. Using woocommerce_product_options_inventory_product_data hook we can add extra fields at the end of options available in Inventory tab. Below is the code snippet which will add “Minimum Quantity” and “Maximum Quantity” options in the Inventory tab.

Minimum and Maximum Quantity Options - set minimum and maximum quantities in WooCommerce
Minimum and Maximum Quantity Options

Next thing we need is saving the above options. Currently, they are only being shown on the Inventory section, but they won’t be saved if you enter values.

To save the values set to these options we will have to use woocommerce_process_product_meta action hook.

When Publishing/Updating the product, the values set to “Minimum Quantity” and “Maximum Quantity” option will get saved in _wc_min_qty_product and _wc_max_qty_product meta keys of the post in the post_meta table.

Saved values in Minimum and Maximum Quantity - set minimum and maximum quantities in WooCommerce
Saved values in Minimum and Maximum Quantity

In the beginning of this post, we saw that we changed the minimum and maximum value for the quantity fields using the filter with some static values. In the below code we are fetching the values set to “Minimum Quantity” and “Maximum Quantity” and assigning it dynamically to arguments of quantity selector field.

Let’s understand what exactly is done in the above code snippet.

woocommerce_quantity_input_args is the filter which is used to alter the values being passed to create quantity field on the front end of the product page. Using this filter, in the callback wc_qty_input_args() function, we are setting the new values for min_value and max_value based on the values set in the “Minimum Quantity” and “Maximum Quantity” options.

You can now use the quantity limitations on the WooCommerce product page, which is only 1 part of the task.

Our second part is to have some validation on minimum and maximum quantity of the item when it is added to the cart and when it gets updated in the cart.

Validation on Add to Cart action

Let’s pause for a minute & think on why we need to validate on the add to cart action? For example, you have set the maximum quantity to 5 for Product A and you have added Product A for 5 quantity (max allowed quantity) in your basket. Now again you are adding Product A with 5 quantity and you are allowed to do this because there are no validations when the customer clicks on the “Add to Cart” button. That is why validation on the add to cart action is needed. Let’s see how we can do that.

WooCommerce has the woocommerce_add_to_cart_validation filter using which we can validate the add to cart action based on required conditions. As you can see in the below code snippet I have used the same filter. I

In the callback function wc_qty_add_to_cart_validation() I am checking that if the total quantity of the product in the cart is higher than the maximum quantity set for the product, then do not allow to add the product to the cart and display a WooCommerce error message.

 

Validation on the Add to Cart button click action - set minimum and maximum quantities in WooCommerce
Validation on the Add to Cart button click action

Validation on Updation of quantity in Cart page

Again coming to the same question, why we need to validate on Update Cart button action in the Cart page?

Because the customer can add the Product A to cart for 5 quantity and he is allowed to update the quantity higher than the value set to Minimum and Maximum Quantity options.

WooCommerce provides woocommerce_update_cart_validation filter to add the update cart action validations. Below is the code snippet which will do this validation.

As you can see in the above code snippet I have used the woocommerce_update_cart_validation filter to performed some validations based on the minimum and maximum quantity set for the product and total quantity of that particular product available in the cart.

After this, the user cannot update the cart with quantity less than the value set to “Minimum Quantity” option and greater than value set to “Maximum Quantity” option.

In this post, we learn how to set minimum and maximum allowable product quantities to be added in WooCommerce Cart using custom code. You can add some more additional options or the conditions based on your business requirements.

Free & Premium WooCommerce plugins to set minimum & maximum quantity for products

There are paid plugins available that come with many other options to play with minimum and maximum allowed quantity and its rules. You can check Minimum and Maximum Quantity for WooCommerce plugin which allows setting Min/Max Quantity rule at the product level as well as at cart level. And it also allows setting minimum and maximum cart amount rules.

Below is the list of the free plugins available on WordPress.org which allows to set minimum and maximum allowable product quantities to be added in WooCommerce Cart.

1. Min and Max Quantity for WooCommerce
2. WooCommerce Minimum and Maximum Quantity
3. WooCommerce Max Quantity

Quick summary:

Limiting the minimum & a maximum quantity of product can be achieved by using the filters provided by WooCommerce. Also, we have seen in this post that using some custom code snippets (including the filter available in WooCommerce) you can create proper interface and validations for allowing minimum and maximum quantity for the product. Or you can install already available plugins on your WordPress Repository and achieve the same.

In our Booking & Appointment plugin for WooCommerce, we added a feature recently where we are limiting the maximum allowed quantity for a product to be booked based on the Maximum booking set in our plugin. That’s where I got the idea to write this as a post.

Maximum quantity based on Maximum Bookings - set minimum and maximum quantities in WooCommerce
Maximum quantity based on Maximum Bookings

I hope this post helps you to learn how things in the WooCommerce can be easily customizable. If you have any questions then feel free to comment it.

Browse more in: WooCommerce, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
86 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Pedro Sousa
5 years ago

Really good!

It’s possible to change the quantity for each variable product?

Regards,

friendorfoley
2 years ago
Reply to  Kartik Parmar

Unfortunately, minmax for Woocommerce Doesn’t work on a base level for Variable products. It only works when you have global settings, just like this one!

Eric
4 years ago

Is it possible to set max quantities by country being shipped to?

4 years ago

Can you set max for Course? I don’t see an easy way to do this and my clients are mistakenly adding the course to their cart more than once.

frzr
4 years ago
Brad
4 years ago

Thanks. This is awesome, and most importantly, it works! One suggestion:

You should namespace your functions, e.g. ‘mytheme_wc_quantity_input_min_callback()’. Right now they have the same ‘wc_’ namespace as the Woocommerce functions, which is potentially problematic. It might also be a good idea to namespace the custom field keys.

Thanks again.

4 years ago
Reply to  Kartik Parmar

Was this code updated as it is not working for me!!! 🙁

4 years ago
Reply to  Kartik Parmar

Yes, indeed it works. It seems the theme I was using had modified WoCcommerce hooks. Great job btw!!!

ps: is there any way to make this work with variable products? Not talking about other plugins but make this code usable when variable selected also.

Thank You! 🙂

4 years ago
Reply to  Kartik Parmar

That’s such and Wonderful news!

Thank you!

4 years ago
Reply to  Kartik Parmar

Kartik, did you ever get around to doing the post for variations. Your original code is such a great help, I’m anxious to do the same with variations. Thanks for sharing.

4 years ago
Reply to  Kartik Parmar

Kartik,

Thanks for the help. Unfortunately, I’m having a problem with the code. When I try to add more of an item that is already in my cart, I’m getting the Woocommerce message “You can add a maximum of “item title” to your cart. You already have “number of items in cart.” None of the items has a maximum quantity set. Do you have any thoughts on what could cause this issue?

4 years ago
Reply to  Kartik Parmar

Yes! It worked perfectly. Thank you very much for making this code available. You have provided a wonderful service that is helpful to many. I appreciate it very much.

Thanks again,

Jim

4 years ago
Reply to  Kartik Parmar

Kartik,

The single product code is working great. Today, I tried to use the minimum/maximum code you provided. Unfortunately, I got the white screen of death. Had to go into my file manager to delete the snippet. Not sure where things are conflicting, but everything works again when I disabled that snippet.

4 years ago
Reply to  Rezart

Hi Rezart, Kartik, First of all thanks Kartik for sharing this tuto. I am using Divi theme and put the code in the functions.php page of my child theme. I do see I am now able to add a min and a max in the “inventory” of all products but it doesn’t work when I got to test it. Rezart talked @ hooks (don’t really know what this is precisely). How can I check DIVI did or not change the WC hooks? And if it’s not the “hooks” , can you confirm this post is still up to date? Even… Read more »

86
0
Would love your thoughts, please comment.x
()
x