5 Replies Latest reply on Mar 5, 2009 3:59 PM by (J_Hinckley)

# Calculating shipping costs

I'd like to have shipping costs automatically calculated in an order form. The cost is determined from a table (see below) and is dependent on the amount in the "Sub Total" field.

\$ .01-49.99............\$5.00
\$ 50.00-74.99..........\$6.00
\$ 75.00-99.99..........\$7.00
\$100.00-199.00.........\$8.50
\$200.00+...............\$10.00

For example, If the amount in the "Sub Total" field was \$125, then the shipping charge would be \$8.50. I'd like this amount to be automatically entered into the "Shipping" field.

How can this be done with scripting?

• ###### 0. Re: Calculating shipping costs
You will need to write a custom script and can use an "if" statment, "switch" statement, or loop through an arry;

// using the if statement
var fSubtotal = this.getField('subtotal').value; // get value
if(fSubtotal >= 200) event.value = 10;
else if(fSubtotal >= 100) event.value = 8.5;
else if(fSubtotal >= 75) event.value = 7;
else if(fSubtotal >= 50) event.value = 6
else if(fSubtotal > 0)event.value = 5;
else event.value = 0;

// using the switch statement
var fSubtotal = this.getField('subtotal').value; // get value

switch(true) {

case ((fSubtotal > 0) && (fSubtotal < 50)):
event.value = 5;
break;

case ((fSubtotal >= 50) && (fSubtotal < 75)):
event.value = 6;
break;

case ((fSubtotal >= 75) && (fSubtotal < 100)):
event.value = 7;
break;

case ((fSubtotal >= 100 && fSubtotal < 200)):
event.value = 8.5;
break;

case (fSubtotal >= 200):
event.value = 10;
break;

default:
event.value = 0;
break;
} // end switch

// using an array
// > start price, < end price, shipping cost
var aShipping = new Array([0, 0.01, 0],[0, 5, 5],
[50, 75, 6], [75, 100, 7], [100, 200, 8.5],
[200, 999999, 10]);
// get subtotal value
var fSubtotal = this.getField('subtotal').value;
// loop through he shipping cost array
for(i = 0; i < aShipping.length; i++) {
if (fSubtotal >= aShipping[i][0] & fSubtotal < aShipping[i][1]) {
event.value = aShipping[i][2];
break;
} // end if
} // end loop through the shipping cost array
• ###### 2. Re: Calculating shipping costs
Geo,

I tried all three of your solutions and couldn't get any of them to work.

I copied the code into the Custom calculation script box in the Calculate tab of the Text Field Properties dialog box for the Shipping field.

Since I'm a novice, I'm sure I'm doing something wrong but don't know what. Your help would be much appreciated.
• ###### 3. Re: Calculating shipping costs
Well, I learned one thing. The field name within the parentheses is case sensitive. When I changed it to match the case in the form, it worked fine.

A couple of other observations:

It doesn't appear to make any difference if the field name is enclosed by single or double quotation marks.

The code appears to work with or without the semicolon at the end of each line.

What is the function of the semicolon?
• ###### 4. Re: Calculating shipping costs
The semicolon marks the end of a statement but JS is very lenient and can run the code even without it. And yes, JS is case-sensitive so you have to make sure you are using the exact same name for a variable throughout your script/form.
• ###### 5. Re: Calculating shipping costs
Geo,

Your if statement and switch statement solution worked fine. I can't get your array statement to work though.