Maximum Quantity Check with Javascript

General ShopSite user discussion

Maximum Quantity Check with Javascript

Postby Bethl » Sat Aug 26, 2017 10:06 pm

I have successfully added javascript to check for a maximum quantity in the Shopping Cart for 1 SKU, but have been unsuccessful in having it check for a count on a group of SKUs. Does anyone know how to fix this?

This works for 1 SKU:
Code: Select all
ns_count = 0;
 
 for(ns=0; ns<ss_sku.length; ns++){
    if(ss_sku[ns]=="widget1"){
         ns_count+= ss_quantity[ns];
    }
 }
 
 if (button == "8") {
   if(ns_count > 6){
     alert("Maximum quantity is 6 Widget1 packages per order, please adjust your quantity accordingly.");
     
     return false;
   }
 }

================================
But it will not work for more than 1 SKU (for a group). Tried using OR "||" but it ends up checking random SKUs and shows the alert for SKUs not listed. Example of code that does not work:

Code: Select all
//SKU Group
 ns_count = 0;
 
 for(ns=0; ns<ss_sku.length; ns++){
    if(ss_sku[ns]=="widget1" || "widget2"){
         ns_count+= ss_quantity[ns];
    }
 }
 
 if (button == "8") {
   if(ns_count > 8){
     alert("Maximum quantity for this group is 8 Widget1 or Widget2 packages per order, please adjust your quantities for these two items accordingly.");
     
     return false;
   }
 }


Any help would be appreciated. I do not know javascript well and could not find a solution online.
Thanks!
Bethl
 
Posts: 12
Joined: Fri Feb 04, 2011 8:46 am

Re: Maximum Quantity Check with Javascript

Postby ajeffery » Fri Sep 01, 2017 7:38 am

Our template designer suggested the following:

You could use an extra product field to be able to tie products into groups that way their code doesn’t need to have all possible skus. The merchant would be able to just add the group name into an extra product field (or any text when using my example code below) and it gets added to the quantities being checked.

Then their JavaScript would be something along the lines of ...

ns_count = 0;
for (i = 0; i < ss_field1.length; i++) {
if (ss_field1[i].length>1) {ns_count = ns_count + parseInt(ss_quantity[i]);}
}

For this example they would just type anything in the extra product field 1 for any products that are part of this max quantity setup.
ajeffery
 
Posts: 10
Joined: Thu Sep 29, 2016 12:48 pm

Re: Maximum Quantity Check with Javascript

Postby Bethl » Mon Sep 04, 2017 6:53 pm

Thank you! I will give it a try and see if it works.
Bethl
 
Posts: 12
Joined: Fri Feb 04, 2011 8:46 am


Return to User Forum

Who is online

Users browsing this forum: No registered users and 9 guests

cron