• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
17

Please help non-techy with custom calculation script

Community Beginner ,
Feb 16, 2024 Feb 16, 2024

Copy link to clipboard

Copied

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!

TOPICS
How to , JavaScript , PDF forms

Views

303

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 16, 2024 Feb 16, 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;

Votes

Translate

Translate
Community Expert ,
Feb 16, 2024 Feb 16, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 16, 2024 Feb 16, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 16, 2024 Feb 16, 2024

Copy link to clipboard

Copied

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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 17, 2024 Feb 17, 2024

Copy link to clipboard

Copied

LATEST

It worked! I cannot thank you enough!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines