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

How to restrict the quantity field to selected numbers in WooCommerce

How to restrict the quantity field to selected numbers in WooCommerce - tychesoftwares.com

As a WooCommerce store owner, often, you may want the user to order fixed quantities for certain products. For instance, in case of furniture or home decor stores, products such as chairs may be ordered in sets of 2, sofa pillows may be ordered in sets of 3 or 4 and so on. In case of sets, there will always be a step increment such as 2, 3, 4 depending on how many items exist in that set. There also may be circumstances where you may want the user to order certain products in fixed quantities without step increments.

For instance, consider a scenario where an online wholesale store wishes to sell minimum 20 pieces of one thing (can be any item such as a planter/pot or a clothes hanger). If the user wants more than 20 pieces, the store wants them to place an order for 30 pieces, nothing in between such as 21 or 23. And if they want more than 30, the store may want them to order atleast 45. This may be for various reasons. Note that the quantities here (20, 30, 45) don’t follow a pattern and don’t have a step increment.  So let’s explore in this post how to do this for both scenarios – 1) where the quantities are in step increments, and 2) where the quantities are random. In other words, how to restrict the quantity field to selected numbers in WooCommerce.

How to restrict the quantity field in case of step increments

This is particularly useful when products come in sets. In the example we have chosen for this tutorial, we want the user to order pillows in sets of 3, 6, 9 and so on. One way to do this would be to list the product itself as “Set of 3” and then make the user order in the regular way. The user would do this by simply adding the desired number of sets to the cart. Another way to do this is by restricting the quantity field to start from 3 and increasing the quantity in multiples of 3 when the user clicks on the up arrow or the “+” sign. We’ll see how to do the same using a code snippet. Add the following code snippet to the functions.php file of your child theme.

add_filter( 'woocommerce_quantity_input_args', 'ts_woocommerce_quantity_selected_number', 10, 2 );
  
function ts_woocommerce_quantity_selected_number( $args, $product ) {
 // global $product;
   if ( ! is_cart() ) {
 if ($product->get_slug()=="throw-pillow"){
      $args['input_value'] = 3; // Start from this value (default = 1) 
      $args['max_value'] = 18; // Maximum quantity (default = -1)
      $args['min_value'] = 3; // Minimum quantity (default = 0)
      $args['step'] = 3; // Increment or decrement by this value (default = 1)
 }
   } else {
 if ($product->get_slug()=="throw-pillow"){
      // Cart's 'min_value' is 0
      $args['max_value'] = 18; 
      $args['step'] = 3; 
      $args['min_value'] = 3;
 }
   }
  
   return $args; 
}

After adding our function ts_woocommerce_quantity_selected_number to the hook woocommerce_quantity_input_args, we have defined input, minimum, maximum and step increment values for the quantity field if the desired product is selected, which in our case are pillows. You can get the product slug by visiting the Products tab and clicking on “Edit” below the product of your choice:

restrict the quantity field to selected numbers in WooCommerce - Clicking on Quick Edit under the Product Name

restrict the quantity field to selected numbers in WooCommerce - Product Slug

This will result in the minimum quantity starting from 3:

restrict the quantity field to selected numbers in WooCommerce - Product page reflects the minimum quantity

The quantity will increase in steps of 3, as desired:

restrict the quantity field to selected numbers in WooCommerce - GIF to display the increase in quantities

The ‘else’ part of the code ensures that this happens with the quantity field on the Cart page too:

restrict the quantity field to selected numbers in WooCommerce - GIF to display the increase in quantities on the Cart page.

How to restrict the quantity field in case of fixed numbers

When it comes to fixed numbers which don’t follow a common pattern, we can change the quantity field by using some plugins for the same. One such plugin is the WooCommerce Fixed Quantity plugin which is free of cost and easy to use. Once installed, you can find it under WooCommerce->Settings->Products->Fixed Quantity:

restrict the quantity field to selected numbers in WooCommerce - The WooCommerce Fixed Quantity plugin.

This plugin can be used when you are adding or editing the product. Let us consider an example where the user has to place an order for a minimum of 20 pieces of clothes hangers. Such scenarios can be encountered in wholesale stores, whether online or offline. We will add the product, mention the Regular Price under ‘General’ and click on the Fixed Quantity Price to mention the quantities:

restrict the quantity field to selected numbers in WooCommerce - The Fixed Quantity Price tab under Add New Product

After clicking on “Add Price”, one needs to mention the minimum Quantity (Qty) and Price of an individual piece (Price Per Qty). If you wish to offer a discount, you can mention a different price and the discount will be automatically calculated based on the Regular Price you have entered in the General Tab.

restrict the quantity field to selected numbers in WooCommerce - Adding a fixed quantity

As our quantities for this scenario are random and do not follow a common pattern, we will add a few prices with the desired quantities by clicking on Add Price each time, in this manner:

restrict the quantity field to selected numbers in WooCommerce - GIF of adding more quantities using the Fixed Quantity Plugin

You can update/publish the product once all the required quantites are added like this:

restrict the quantity field to selected numbers in WooCommerce - Quantities added using the Fixed Quantity Plugin

Now the user can only add these quantities to the cart:

restrict the quantity field to selected numbers in WooCommerce - Product Page

You can change how the text here appears, by either editing the Description field when you’re adding the quantities (in order to have a custom description for each quantity), or, if you want to change the text for all quantities, you can edit the template in the Plugin settings by navigating to WooCommerce->Settings->Products->Fixed Quantity. Let us edit the template to display only the quantity and final amount, and not the individual price.

restrict the quantity field to selected numbers in WooCommerce - Editing the Description template under Plugin Settings

You will also need to edit this individually for the product quantities that have already been added (see image below). The new ones will automatically carry this template however.

restrict the quantity field to selected numbers in WooCommerce - Editing the Description template for each quantity

After clicking on Update, you can view the new text on the Product Page:

restrict the quantity field to selected numbers in WooCommerce - Description changed on the Product Page

Though this plugin works and has been tested with WordPress 6.2.2 and WooCommerce 7.8,  it is no longer being updated.
If you’re looking for an alternative plugin, we recommend that you check out the Min Max Quantity & Step Control for WooCommerce plugin, the free version of which allows you to set minimum & maximum quantities for all products, products across specific categories, and also individual products. It also lets you set step increments for quantities.

In this way, you can use a code snippet for quantities with step increments, and plugins for random quantities.

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

Share It:

Subscribe
Notify of
6 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Pedja
3 years ago

I tried the first method but instead values 3, 6, 9, 12, 15, 18 on front-end I get 3, 4, 7, 10, 13, 17

11 months ago
Reply to  Anubha Bhat

Hi there,
Thank you for such an informative blog.

However, the “Woocommerce fixed quantity” plugin seems to be outdated as it hasn’t been updated for years. Do you still suggest this plugin or any other free plugin that does the same job for fixed quantities?

Thank you

Editor
11 months ago
Reply to  Awaaz

Hi Awaaz,
You’re absolutely right. The “Woocommerce fixed quantity” plugin hasn’t been updated for years. And, we have suggested an alternative plugin at the end of this post named  Min Max Quantity & Step Control for WooCommerce plugin which is actively updated and also offers the same fixed quantity functionality as the outdated one.

11 months ago
Reply to  Saranya

Hi Saranya,
Thank you for the response. I must have missed that last paragraph where you suggest the alternative.

BTW, I have checked that “min max quantity & step control for woocommerce” plugin but didn’t find the functionality like “woocommerce fixed quantity” plugin has in the free version.

Please advise. Thanks

Editor
11 months ago
Reply to  Awaaz

Hi,
If you are looking only for the min, max, and step quantity control functionality then it is provided for free in the plugin mentioned in our post, as I have already tried it on my side. Of course, some other functionalities are provided for the pro version. Another plugin that I would suggest you to try out is Min Max Default Quantity for WooCommerce.This allows for more added functionalities such as setting the quantity dropdown & dynamically changing the price according to the quantity selected. Please refer to the screenshot-https://prnt.sc/Jfjt7HuVy9nG

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