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

Conclusion

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
88 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Jakub
1 month ago

Thanks, this works perfectly!

David
2 months ago

Hi,

When adding the last code snippet to validate upon updating the car quantity I got the following error:

Cannot redeclare function wc_qty_get_cart_qty.

Any ideas about what’s going on there and how to fix it?

HanszFranz
9 months ago

hi,
for me it shouldn’t matter how many products are in the shopping cart, it just can’t be 4. What would the snippet look like then?

1 year ago

Hi, I wanted to add a shortcode to the code so I can display the minimum order value quantity on the product shop page. I was not sure where to place the add_shortcode( ‘shortcode_name’ , ‘callback_function’ ).
I was going to place it in this function “function wc_get_product_min_limit()”.

Mohamed
1 year ago

Hi,

thx for the above tutorial, it’s very useful,

but can i do something like this for the minimum & maximum amount (cart & check out)

for different currencies?
as all codes i found or plugin works for the default currency only

THx in advance

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