How to Set a Maximum Quantity On the Checkout Page

How to Set a Maximum Quantity On the Checkout Page
Written by Jonathan Diaz
Updated 3 years ago

This is a fairly simple script so that you can limit the number of a certain item that a customer can add to the cart. Extremely useful when you are dealing with Free+Shipping offers.

It is not foolproof in that if someone already has too many in their cart then they will be able to checkout. However, it will stop them adding further quantity on the checkout page itself.

I would advise you to use it in conjunction with clearing the cart before sending someone to the product page. You do this by adding:

?cart_clear=true

at the end of your URL.

eg. 

https://domain.com/a/offers/f/4000/0/someproducttitle?cart_clear=true

Copy the script below into your checkout page, by clicking on Settings/Custom Script. On the first line, it says: var maximumQuantity=2; You can safely change the number 2 to any quantity that you want.

<script> var maximumQuantity=2;

UpdatePriceButton = function(){

var circleplus= document.getElementsByClassName("fa-plus-circle");

var priceEls = document.getElementsByClassName("cart_product_count");

for (var i = 0; i < priceEls.length; i++) {

if (typeof priceEls[0] !== 'undefined') {

var price = priceEls[i].innerText;

if (price > maximumQuantity) {

  circleplus[i].style.display = 'none';

}

}

}

};

   

window.setTimeout(function(){UpdatePriceButton()}, 500);

update_cart_info_without_refresh  = (function() {

var cached_function = update_cart_info_without_refresh ;

    return function() {

        var result = cached_function.apply(this, arguments); // use .apply() to call it

var scriptCheck = setInterval(function(){

     if(typeof update_cart_info_without_refresh == 'function') {

var circleplus= document.getElementsByClassName("fa-plus-circle");

var priceEls = document.getElementsByClassName("cart_product_count");

for (var i = 0; i < priceEls.length; i++) {

if (typeof priceEls[0] !== 'undefined') {

var price = priceEls[i].innerText;

if (price > maximumQuantity) {

  circleplus[i].style.display = 'none';

}

}

}

        

         clearInterval(scriptCheck);

     }

}, 500);

      

        return result;

    };

})();</script>

Did this answer your question?