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.
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.
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.
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.
Increase WooCommerce Store Sales
"Abandoned cart pro is worth every penny. We have successfully recovered about 17k just this month in sales. The extra benefit of exporting and diagnosing potential sales and areas where we need improvement are immense. An extra special thanks to David Katzner and the developers for their attention to some custom requests we had asked for." - Kenny Rhoads at MultiTable
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 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
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.
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.
It’s possible to change the quantity for each variable product?
Yes, it is possible. You can check Min and Max Quantity for WooCommerce plugin which is freely available on WordPress repository. It allows limiting the quantity of product variations as well.
Please let me know if you have any further question.
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!
Is it possible to set max quantities by country being shipped to?
Not sure if by adding some more code, max quantity can be set by the country being shipped to or not.
But you can check ‘Conditional Shipping and Payments’ plugin which allows adding conditions on product items based on shipping settings. You can contact plugin author for detailed information.
Please let me know if you have any further questions.
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.
Have you gone through this post? It’s all about setting minimum and maximum quantity for product(course).
You can use the code snippets shared in this post if you have some knowledge of coding or you can install the plug-in mentioned in this post to achieve your requirements.
If you want your clients to add the product only once to their basket then WooCommerce has Sold Individually option available under Inventory tab of Product data meta box. Please let me know if that will help or not.
this plugin did what you need : https://codecanyon.net/item/woocommerce-limit-order/16478761?ref=frzr
Thanks for the sharing the link. But like I have mentioned in this post that there are already the free plugins available on WordPress Repository then it is meaningless to purchase the plugin.
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.
You are welcome. And I am glad to know that it worked for you. 🙂
You know what, mostly I use the prefix for the function name but in this post I missed it. 🙁
No worries. Soon I will update the gist with the prefix to the function.
Thank you for the suggestion. Have a great day. 😉
Was this code updated as it is not working for me!!! 🙁
I just rechecked the code snippet with the latest version of the WooCommerce and everything is working fine for me. It would be great if you can let me know how exactly you are trying to use this code and what issue you are facing so that it will help me to identify what is going wrong at your end.
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! 🙂
I am glad to know that it worked for you. 🙂
Yes, I am going to write a separate post on setting minimum and maximum quantity for a variable product as well. Hopefully the same code I can reuse to achieve the same. Fingers crossed. 😉 Once the post is published then I will let you know of the same.
Please let me know if you have any further questions or suggestions.
That’s such and Wonderful news!
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.
I started but I could not spend more time on it so the post is still pending. But I have managed to create some snippets for it and I hope that might will help you in some sense. Below is the link for the code snippet which will add the Minumum & Maximum Input fields for each variation and apply the settings on the front end.
Please let me know if you have any further questions.
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?
Ohh yes, I am able to replicate the same at my end. When no value set in the maximum quantity field then it is showing error notice when adding the product to cart. Applied the patch on line number 35 of the code snippet available on below link for validating the cart.
Please do the changes and let me know if that resolves the issue or not.
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.
You are welcome. 🙂 I am glad to know that it works for you.
The code snippet provided for the Minimum & Maximum Quantity for Variations is not thoroughly tested so it might be possible you may face some issues. So feel free to let me know if you face any issue.
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.
I apologize for the inconvenience you are facing. Would you mind sharing below admin level access details so that I can debug the issue? I would need access to your WordPress website and FTP details of it. Please share the same on below email address.
Email Address: kartik at tychesoftwares dot com
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 »
Hi Andre, You are welcome. 🙂 Yes, the code snippets are updated. I have not checked the same with the latest version DIVI theme so I am not sure why min and max are not being applied on the front end. I am assuming that you already know that this snippet will work only for the WooCommerce Simple Product type. If you can send the latest version of DIVI theme to below email address then I will check if the same thing is replicated at my end not. If replicated and that can be fixed quickly then I will get… Read more »