Skip to main content
Participant
February 16, 2024
Answered

Please help non-techy with custom calculation script

  • February 16, 2024
  • 1 reply
  • 1257 views

I have a photography order form (attached) where if the following sized items are ordered, then shipping and handling is $15.99: 10x20, 8x16,  10x30, 8x24 and 5x15 (each field's total is named is named Total - e.g., 10x20Total). All other sizes shipping and handling would be $7.99. So I'm assuming the code would be if any of the first sets of sizes is greater than 0, then S&H is $15.99; otherwise smaller sizes S&H is $7.99. 

 

Just so you know, after reading through other posts, I did enter if(this.getField("10x20Total").value > 0)event.value=15.99 as a test, but when I deleted the order for a 10x20, the $15.99 remained. 

 

I've attached a copy of the order form, and greatly appreciate any assistance with this!

This topic has been closed for replies.
Correct answer Nesa Nurani

Use this:

var f1 = ["10x20", "8x16", "10x30", "8x24", "5x15"];
var f2 = ["11x14", "8x10", "5x7", "4x6", "Wallets", "5x10", "Magnet", "LuggageTags", "Keychain"];
var shipping = 0;

function checkFields(fields, cost) {
 for (var i=0; i<fields.length; i++) {
  if (this.getField(fields[i]).valueAsString !== "") {
   return cost;}}
    return 0;}

shipping = checkFields(f1, 15.99) || checkFields(f2, 7.99);
event.value = shipping;

1 reply

Nesa Nurani
Community Expert
Community Expert
February 16, 2024

You need to use 'else' to set field to empty:

if(this.getField("10x20Total").value !== "")
event.value = 15.99;
else
event.value = "";

 

You will need to  add another condition to check for second shipping 7.99

Sarah NNAuthor
Participant
February 16, 2024

Thank you so much for providing this answer! What would I use if I need multiple values be part of the $15.99 shipping (e.g., 10x20, 8x16,  10x30, 8x24 and 5x15)? Again, I'm not a coder so I just don't know these things.

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
February 17, 2024

Use this:

var f1 = ["10x20", "8x16", "10x30", "8x24", "5x15"];
var f2 = ["11x14", "8x10", "5x7", "4x6", "Wallets", "5x10", "Magnet", "LuggageTags", "Keychain"];
var shipping = 0;

function checkFields(fields, cost) {
 for (var i=0; i<fields.length; i++) {
  if (this.getField(fields[i]).valueAsString !== "") {
   return cost;}}
    return 0;}

shipping = checkFields(f1, 15.99) || checkFields(f2, 7.99);
event.value = shipping;