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
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.
Also, there are paid plugins available which comes with many other options to play with minimum and maximum allowed quantity and its rules. WooCommerce itself have one paid extension called Min/Max Quantities. As well as you can check WooCommerce – Minimum/Maximum Quantities plugin which is available on CodeCanyon.
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.