Max Quantity For WC Shops – Documentation

UPDATE: In June of 2024, this plugin in the WordPress repository was taken over by a third party without my consent because I didn’t respond quickly enough to an email from the WP Plugin people. It seems I responded one day late so they gave my plugin away, removed my committer access, and removed it from my list of created plugins on WordPress. Therefore, as of June 2024, this page no longer represents the plugin on the WordPress plugin directory.

WooCommerce Max Quantity Max Quantity For WC Shops 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 Max Quantity For WC Shops 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” “Max Quantity For WC Shops” to find the plugin.
  3. When you see it, click “Install Now” to install the plugin.
  4. Click “Activate” to activate the plugin.

Top ↑

Settings

WooCommerce Max Quantity Max Quantity For WC Shops 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 Max Quantity For WC Shops 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 July 26, 2024

We've 16 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

    Juan Luis Arias Z.
  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

    Mieke
    • 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.

      Isabel
  3. December 18th, 2017 at 4:55 am

    I want to limit the order to a maximum of 3 items which works great when I set the global value.
    However if I add more than 1 of the same product it won’t let me.
    How can I have a global limit of 3 but no limit to individual products?

    Brett
    • December 18th, 2017 at 12:25 pm

      If I understand you correctly, you cannot add more than 1 of the same product to the cart? This means that you probably enabled “Sold individually” for that product. To find the “Sold individually” setting, edit the product, look under “Product data”, then look in the Inventory tab. There, you can disable that setting.

      Isabel
  4. October 1st, 2018 at 6:59 am

    Hi Isabel

    Thanks for making this awesome simple plugin.

    I’ve customised my shop page to show plus/minus buttons for adding products to the basket. Is there a way to hook in the max quantity set in your plugin into the js-code I wrote for these plus/minus buttons (i.e. to show a message when hitting the max amount in the amount selector).

    Thanks
    Peter

    Peter Jensen
  5. September 21st, 2021 at 2:20 pm

    Hi:

    We’re a not-for-profit that distributes free trees to residents in our city.

    To ensure fair access to a limited number of trees, we want to limit the number of trees that can be ordered (customers have a choice of 8 species) to a total of 2 trees per person.

    We’ve installed the plugin and we see the messages you described. However, when someone has tried to order more than 2 trees, the cart seems to have disappeared – i.e., no link or button.

    What can you suggest?

    Ron Jamieson
  6. November 9th, 2021 at 10:19 am

    Hi, just to let you know that you plugin seems to generate a conflict with Woocommerce 4.8.
    In fact i checked the option to redirect to cart after purchase. This only works for the 1st purchased item, then I stay on the product page for the second item.

    Just to let you know and maybe correct this.
    Thanks for your dedication.
    Regards

    Eric

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]