Using CheckIt function to block certain ship-to states

General ShopSite user discussion

Using CheckIt function to block certain ship-to states

Postby LostInCode » Wed Feb 11, 2009 2:51 pm

Hello! I need some external brain power to help me with a simple code problem. :shock:

By Federal law, I need to deny shipping to certain states. I have tested this snippet of code in the "CheckIt function" box of the Commerce Setup>Order System>Checkout page.

Code: Select all
if (document.billing.ShipState != "AL")
      {
      alert("Sorry, we cannot legally ship to this state.");
      return(false);
}


It works perfectly, but it only blocks one state - Alabama. I have 11 states I need to block. I have tried variations of this code to block all states, but it either seems to block all states or none at all.

Here's one of the variations I used. Don't laugh.

Code: Select all
if (document.billing.ShipState != "AL", "AR", "KS", "ME", "MD", "MA", "MS", NJ", "OK", "PA", "UT") {
      alert("Sorry, we cannot legally ship to this state.");
      return(false);
}


How can the first (working) code example be altered to allow for blocking all 11 states?

Thanks for looking. I hope someone has a simple solution. :D

.
LostInCode
 
Posts: 14
Joined: Fri Feb 22, 2008 10:28 am

Postby Jim » Wed Feb 11, 2009 4:11 pm

You probably need to use a "switch" or "case" statement. Do a web search for "javascript case" and you should get some hits of examples.
Jim
Site Admin
 
Posts: 4953
Joined: Fri Aug 04, 2006 1:42 pm
Location: Utah

"javascript case"

Postby LostInCode » Wed Feb 11, 2009 4:21 pm

Thanks, but that was like lesson in advanced Greek. :?

Anyone have an actual example in ShopSite-friendly language?

.
LostInCode
 
Posts: 14
Joined: Fri Feb 22, 2008 10:28 am

Postby Jim » Wed Feb 11, 2009 4:43 pm

Try something like this.

Code: Select all
switch(document.billing.ShipState.value)
{
case "AL":
case "AR":
case "KS":
case "ME":
case "MD":
case "MA":
case "MS":
case "NJ":
case "OK":
case "PA":
case "UT":
   alert("Sorry, we cannot legally ship to this state.");
   return(false); 
 

default:
   return(true);
}


You might need to assign document.billing.ShipState to a variable first and then use that var where that text is in the statement.
Note that I updated the code because I left out a " on one of the states.
PPS. Feb 12 2009 I corrected the example to include the .value for the state in case someone finds this example and tries to use it. I tested it to make sure it works and it does.
Last edited by Jim on Thu Feb 12, 2009 8:25 am, edited 1 time in total.
Jim
Site Admin
 
Posts: 4953
Joined: Fri Aug 04, 2006 1:42 pm
Location: Utah

Oh boy. No comprende.

Postby LostInCode » Wed Feb 11, 2009 6:18 pm

You might need to assign document.billing.ShipState to a variable first and then use that var where that text is in the statement.


Que? Is there a how-to in the ShopSite documentation? I've looked on Google, but the samples look very different from what is currently in use on ShopSite (ex. when viewing page source).

Thanks for your patience and ideas so far. I think it's on the right track.

.
LostInCode
 
Posts: 14
Joined: Fri Feb 22, 2008 10:28 am

Postby robm » Wed Feb 11, 2009 6:37 pm

Try this (to block AL and AK for example):

Code: Select all
if (document.billing.ShipState == "AL" || document.billing.ShipState == "AK")
      {
      alert("Sorry, we cannot legally ship to this state.");
      return(false);
}

Add as many states to the example above using this format and it should work.
robm
 
Posts: 463
Joined: Fri Aug 04, 2006 5:46 pm
Location: Connecticut

Darn - didn't work

Postby LostInCode » Wed Feb 11, 2009 10:06 pm

Rob,

Thanks for the code. I copied/pasted into the CheckIt box. With the code:

Code: Select all
if (document.billing.ShipState == "AL" || document.billing.ShipState == "AK")
      {
      alert("Sorry, we cannot legally ship to this state.");
      return(false);
}


no states were blocked at all. All states were approved.

When I tried this alternate with !=:

Code: Select all
if (document.billing.ShipState != "AL" || document.billing.ShipState != "AK")
      {
      alert("Sorry, we cannot legally ship to this state.");
      return(false);
}


all states were blocked, including states not even mentioned in the above code.

Any ideas as to why it's giving an "all-or-nothing" result?

I even tried the following code with an else if as a total guess (not that I know how to do that), and it gave me the same thing - either all states approved, or all states blocked.

Code: Select all
if (document.billing.ShipState != "AL") {
      alert("Sorry, we cannot legally ship to this state.");
      return(false);
}
else if (document.billing.ShipState == undefined){
      return(true);
}


What the heck? Isn't there a way to block only certain states?

.
LostInCode
 
Posts: 14
Joined: Fri Feb 22, 2008 10:28 am

Postby robm » Thu Feb 12, 2009 6:30 am

Sorry, try this:

Code: Select all
if (document.billing.ShipState.value == "AL" || document.billing.ShipState.value == "AK")
      {
      alert("Sorry, we cannot legally ship to this state.");
      return(false);
}


The ".value" part was missing.
robm
 
Posts: 463
Joined: Fri Aug 04, 2006 5:46 pm
Location: Connecticut

Postby Jim » Thu Feb 12, 2009 8:01 am

I just tried the code that I previously submitted and it works if you put the .value for the state. So here is the code that will work.
Code: Select all
switch(document.billing.ShipState.value)
{
case "AL":
case "AR":
case "KS":
case "ME":
case "MD":
case "MA":
case "MS":
case "NJ":
case "OK":
case "PA":
case "UT":
   alert("Sorry, we cannot legally ship to this state.");
   return(false);
 

default:
   return(true);
}
Jim
Site Admin
 
Posts: 4953
Joined: Fri Aug 04, 2006 1:42 pm
Location: Utah

Woohoo! It Works!!

Postby LostInCode » Thu Feb 12, 2009 10:29 am

Woohoo! It Works!!

Code: Select all
10  PRINT "THANK YOU"
20  GOTO 10
RUN

THANK YOU
THANK YOU
THANK YOU
THANK YOU
THANK YOU
THANK YOU
THANK YOU
THANK YOU
THANK YOU
THANK YOU
THANK YOU
THANK YOU

Seriously - that's a huge load off my shoulders. Thank you both for all your help. :D

.
LostInCode
 
Posts: 14
Joined: Fri Feb 22, 2008 10:28 am


Return to User Forum

Who is online

Users browsing this forum: No registered users and 118 guests