Search the site:

Home Blog How to restrict the quantity field to selected numbers in WooCommerce

How to restrict the quantity field to selected numbers in WooCommerce

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

In this way, you can use a code snippet for quantities with step increments, and the WooCommerce Fixed Quantity plugin where the quantities are random.

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

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
from purchased

Subscribe Now