WooCommerce Max Quantity Documentation

WooCommerce Max Quantity is a plugin for WooCommerce that helps you control your inventory. It let’s you set a maximum quantity limit for each product, per order. This is useful if you want to control your WooCommerce inventory: for example, if you don’t want to let one customer purchase all of your inventory at once.

You can read about the WooCommerce Max Quantity features on the plugin’s page.

Jump down to:

Installation   |   Settings   |   Variety of Ways To Use   |   Customizing   |   Troubleshooting   |   Questions & Comments   |   Ask a Question

Installation

  1. To install the plugin, go to your WordPress dashboard –> Plugins –> Add New.
  2. Search for “WooCommerce Max Quantity” to find the plugin.
  3. When you see “WooCommerce Max Quantity”, click “Install Now” to install the plugin.
  4. Click “Activate” to activate the plugin.

Top ↑

Settings

WooCommerce Max Quantity has only two settings. You can use these settings in a variety of ways to accomplish the unique goals of your store. You can use either one setting, or the other, or both.

The two settings are:

  1. The Universal Max Limit
    A universal limit means that this limit affects all products in your WooCommerce store. This is the max quantity limit that can be added to the cart, per each product. The universal limit works like this: for example, if you set it to 3, then people can only add a maximum of 3 of each product to their cart.

    WooCommerce Maximum Quantity Limit Per Product Setting

    This is the universal max setting. It is labeled, “Maximum Quantity Limit Per Product.”

    To find this setting, go to WooCommerce -> Settings -> Products tab. Click “Inventory”. Scroll down to “Maximum Quantity Limit Per Product.” Set your desired limit, there.

    Each product can override this setting from its own product inventory tab (see the next setting).

  2. The Individual Product Max

    The individual product max limit is a quantity limit that is only set for a single product. This lets you set a different limit for each product. If you set this limit for a product, then that product’s own limit will always override the universal limit.

    Woocommerce Individual Product Max Quantity Per Order Setting

    An individual product’s “Max Quantity Per Order” setting.

    To find this setting for an existing product, go to the product’s own “Edit product” page. (In your WordPress dashboard, go to “Products” then click one of your products to edit it.) (You will also see this setting when adding a new product.) Scroll down to the “Product Data” box. Click on the Inventory tab. There, you’ll see the setting called, “Max Quantity Per Order.” Set your desired max limit for that product, there. You must remember to click “Update” to save your changes.

Top

Variety of Ways To Use

The following examples are meant to explain the possibilities available for putting a limit on quantities because every store has unique needs. WooCommerce Max Quanity helps you control the quantity per order, per each product in several ways. This, in turn, helps you control your inventory.

You can use this plugin in several ways. For example:

  • You can set the universal limit to affect all products, and then set the individual product limit for only a few products.

    The products which have their own limit set will use their own limit, rather than the universal limit. All other products will use the universal limit.

  • You could NOT set a universal limit (just leave it blank). For example, if you don’t want any limit set on most products, and you only want to set a limit on one or two products. You can do that by setting the limits only for those products, individually, and don’t set a universal limit.

  • You could just set the universal limit, and never use the individual product limits. The universal limit will automatically place the limit on all products.

Top ↑

Customizing

Customizing The Error Messages

There are two types of error messages.

  1. One type is a small pop-up message that will appear on the quantity input field if you manually type a number that is too large.

    If someone tries to manually change the quantity to a number higher than the max, the browser will show a pop up message. These messages are set by the browsers, not by WooCommerce or this plugin.

    For example, if you have the max set to 5, and someone tries to add 99 items to the cart, Chrome will show this message:

    “Please select a value that is no more than 5.”

    Firefox will show this message:

    “Value must be less than or equal to 5.”

    If you would like to customize this message, you can do it with JavaScript. Here is code that will change the message to:

    99 is too many. Please call 555-555-5555 to order more than 5.

    You can edit that message on line 14. This code goes in your functions. This code will add the necessary JavaScript to your WooCommerce pages.

    /**
     * Customize the browser's default constraint validation message for exceeding the input max
     */
    function isa_wc_max_qty_scripts() {
    
    	/* Add JS to frontend wherever woocommerce.js is loaded. */
    
    	wp_add_inline_script( 'woocommerce', 'window.onload = function(){
    
    		function onBlurHandler( event ) {
    		    var max = this.getAttribute( "max" );
    			if ( this.validity.rangeOverflow ) {
    
    				message = this.value + " is too many. Please call 555-555-5555 to order more than " + max + ".";
    
    		        this.setCustomValidity( message );
    
    			} else {
    				this.setCustomValidity("");
    			}
    		    
    		}
    
    		var quantity = document.querySelector( "form.cart .qty" );
    		var cartQuantity = document.querySelector( "form.woocommerce-cart-form .qty" );
    		
    		if ( quantity ) { // quantity input on single product page
    			quantity.addEventListener( "blur", onBlurHandler, false );
    		}
    		if ( cartQuantity ) { // quantity input on cart page
    			cartQuantity.addEventListener( "blur", onBlurHandler, false );
    		}
    
    		};'
    	);
    }
    
    add_action( 'wp_enqueue_scripts', 'isa_wc_max_qty_scripts', 9999 );
    

    (Note: the error message above only allows text, no HTML. If you want to add HTML to the message, see this.)

  2. The other type of error message is controlled by the plugin. This is the main error that will appear at the top of the page after they try to add too many items to the cart. This will usually happen if they already have some items in the cart, and then they try to add more items to exceed the max limit.

    This error message looks like this:

    You can add a maximum of 5 PRODUCT_NAME to your cart.”

    To customize this error message, you have to add the following small bit of code to your functions.

    You can edit the message on line 6. The $max variable gives you the maximum quantity allowed. You can use that variable in your error message.

    /**
     * Customize the error message for adding too many items to the cart.
     */
    function my_custom_max_error_message( $message, $max ) {
    
    	return "This is a custom message. You can only add $max of this item to your cart.";
    
    }
    
    add_filter( 'isa_wc_max_qty_error_message_already_had', 'my_custom_max_error_message', 10, 2 );
    add_filter( 'isa_wc_max_qty_error_message', 'my_custom_max_error_message', 10, 2 );
    

    If you don’t want to completely replace the error message, but you rather add to the default message, then use one of the following instead.

    To insert a custom message before the text of the default error message (edit line 6):

    /**
     * Insert custom message before the default error message.
     */
    function my_custom_max_error_message_before( $message, $max ) {
    
    	$insert = 'Message to be inserted before default message. ';
    
    	return $insert . ' ' . $message;
    
    }
    
    add_filter( 'isa_wc_max_qty_error_message_already_had', 'my_custom_max_error_message_before', 10, 2 );
    add_filter( 'isa_wc_max_qty_error_message', 'my_custom_max_error_message_before', 10, 2 );
    

    To add a custom message at the end of the default error message (edit line 6):

    /**
     * Add custom message to the end of the default error message.
     */
    function my_custom_max_error_message_after( $message, $max ) {
    
    	$add = 'Message to be inserted after default message. ';
    
    	return $message . ' ' . $add;
    
    }
    
    add_filter( 'isa_wc_max_qty_error_message_already_had', 'my_custom_max_error_message_after', 10, 2 );
    add_filter( 'isa_wc_max_qty_error_message', 'my_custom_max_error_message_after', 10, 2 );
    

Top ↑

Troubleshooting

Here are some points to consider when troubleshooting a problem with this plugin.

  • If you go back and set an individual product limit on an existing product, you must remember to click “Update” to save your changes.

  • If you enter a limit of “0” it will be considered as a blank setting, which means that there is no limit. (A blank setting is considered as “no limit.”) So, setting the limit to “0” is useless for this plugin. (If you want to stop a product from being purchased, even temporarily, the correct and standard way to do that with WooCommerce would be to mark the item “out of stock.” You don’t need this plugin to do that.)

Top

Updated on October 7, 2017

We've 5 Responses

  1. February 11th, 2017 at 10:54 am

    Hi! Is there a way to make that the Maximum quantity be defined by the Available stock? For example a universal rule that doesn’t let you add yo cart more than 50% of the current stock?
    Thanks

    avatar
  2. April 24th, 2017 at 8:15 am

    Hi Isabel,
    Thanks for the plugin. Is it possible to add max quantity in variable products, in the product variations.
    That would be great!
    Thanks

    avatar
    • April 28th, 2017 at 1:19 am

      Hi. For now, you can add the max quantity to the product. This will be enforced with any variations. But, adding a separate max quantity to each variation is not supported yet. I will add this to a future release.

      avatar

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]