Limiting the Maximum Quantity of Products in ShopSite

This is an update to an earlier tutorial we posted here, with a script to prevent customers from purchasing more than a specified quantity of a product.

This update can handle multiple products, and it allows you to set different maximum quantities for each. If a customer exceeds the quantity of a restricted product customers will not be allowed to check out until all product quantities meet your restrictions.

You would add the following script under Commerce -> Order System -> Shopping Cart – “JavaScript added at start of built-in CheckIt function”. You would use an extra product field to enter the maximum quantity that can be purchased for each restricted product (leave the field blank if a product is not restricted). Finally, you would modify the script and replace the number ‘5’ in ss_field5 with the extra product field you are using in two places (for example, ss_field5 becomes ss_field25 if you are using product field 25). Be careful only to replace the number.

if (parseInt(button)==8) {
   var lineItems = {};
   var limitSkus = false;
   var limitList = {};
   var skuList = new Array();

   for(ns=0; ns<ss_field5.length; ns++){="" if(parseint(ss_field5[ns])=""> 0){ 
          var sku = ss_sku[ns];
          limitList[sku] = parseInt(ss_field35[ns]);
          if(lineItems[sku] > 0){
            lineItems[sku] += parseInt(ss_quantity[ns]); 
          } else{
            lineItems[sku] = parseInt(ss_quantity[ns]); 
          }
      } 
    } 

   ss_jQuery.each(lineItems, function( sku, qnty ) {
      var limit = limitList[sku];
      if(qnty > limit){
         limitSkus = true;
         skuList.push(limit + " of sku: " + sku);
      }
   });
   
   if(limitSkus == true){
      if(skuList.length > 1){
         alert("You may only order: \n\n" + skuList.join("\n") + "\n\nPlease adjust your quantity accordingly");
      } else{
         alert("You may only order: \n\n" + skuList[0] + "\n\nPlease adjust your quantity accordingly"); 
      }
      return false;
   }
}

Looking for a web host that understands ecommerce and business hosting?
Check us out today!

Leave a Reply