Home > How to... > Script Library > Force maximum quantity

Force maximum quantity

You need to paste this script in Design -> Product Detail Design.

<script>
// the four values below are all in in milliseconds
// 1000 milliseconds = 1 second
// Time for the add to cart button to appear again
var add_to_cart_hidden = 1500
// Speed of the fade in of the message
var message_fadein = 500
// Delay for the message to start dissapearing
var message_fadeout_start = 500
// Speed of the fade ot of the message
var message_fadeout = 1000
 
    var target_button = '#btn_AddToBasket';
    var target_text = 'input#txt_Quantity';
    var limit_max
 
maximum_quantity = function(limit_max) {
    if(limit_max != null) {
    $(target_button).hover(
        function() {
            value = parseInt($(target_text).val());
            if ( value > limit_max ) {
                $(target_button).hide();
                $('#error_message').fadeIn(message_fadein);
                $(target_text).val(limit_max);
                setTimeout( function(){
                    $(target_button).show();
                }, add_to_cart_hidden);
            } else {
                return false;
            }
        },
        function() {
        setTimeout( function(){
            $('#error_message').fadeOut(message_fadeout);
        }, message_fadeout_start); 
        });
    } else {
        return false;
    }
}
     
$(document).ready( function() {
    limit_max = $('input#maximum_quantity').attr('value');
    // You can chage the default error message here
    var message =  'Maximum quantity for this item is '+limit_max+'.</br>The value has been reset';
    $(target_button).parent().parent().append('<div id="error_message" style="display: none;">'+message+'</div>');
    $(target_text).val(limit_max);
     
});
 
$(function() {
    maximum_quantity(limit_max);
});
</script>

Then save. Once that's done, all you need to do is put in the marker show below in your product description. It is important that you do it on source mode and that it does not contain anything else then the number representing the maximum quantity you want to impose on that product. In this example it's 9999.

<input id="maximum_quantity" type="hidden" value="9999" />

Then go to the css tab and paste in the following code on the end of that file. It's used to control the look of the message shown by this script.

div#error_message {
    position:absolute;
    top:40%;
    left:40%;
    z-index:10000000;
    background-color:white;
    color:black;
    padding:20px 10px;
    border:1px solid black;
    text-align:center;
}


Translate and Localize your website with 'Localizer pluggin

Shopping Cart Software by Ashop