4 Replies Latest reply on Dec 18, 2013 9:25 AM by zkilmer

    Calculate shipping charges using javascript

    zkilmer

      I have a pdf order form that I am creating to email customers.  I need to create a shipping field in which the value is a flat rate depending on the subtotal.  Example if subtotal is less than $150 then shipping = $15.  If $250 > subtotal > $150 shipping = $20. If $500 > subtotal > $250 shipping = $25 and if subtotal > $500 shipping is free.  I have no experience with javascript but based on some online research I have come up with this:

       

      var sub = this.getField("subtotal").value;

       

      if(sub<"150")

      event.value = 15;

      else if("250">=sub>="150")

      event.value = 20;

      else if("500">sub>"250")

      event.value = 25;

      else

      event.value = 0;

       

      I always get a return value of "0" no matter what the "subtotal" field value is.  Any help is greatly appreciated.

        • 1. Re: Calculate shipping charges using javascript
          try67 MVP & Adobe Community Professional

          If you place quotes around something it is considered text and can't be used in a numeric comparison. Also, you can't include more than one comparison operator in a single expression. Try this code instead:

           

          var sub = Number(this.getField("subtotal").value);

          if (sub==0)

              event.value = "";

          else if (sub<150)

              event.value = 15;

          else if (sub<=250)

              event.value = 20;

          else if (sub<500)

              event.value = 25;

          else

              event.value = 0;

           

          I've added the first condition to make sure the field is empty when nothing is selected.

          • 2. Re: Calculate shipping charges using javascript
            Test Screen Name Most Valuable Participant

            I can see several issues.

             

            1. You are comparing strings, not numbers. Strings are ordered "2" "24" "250" "26" "3", not what you want.

             

            2. You write ("250">=sub>="150") but you cannot do such a three way comparison in JavaScript. You must do two separate comparisons, probably linked with && (and) or || (or).

            • 3. Re: Calculate shipping charges using javascript
              gkaiseril MVP & Adobe Community Professional

              Careful ordering of the comparisons might make this easier and a switch statement is not as confusing as nested if statements.

               

              function GetField(cName) {
              var oField = this.getField(cName);
              if(oField == null) app.alert("Error accessing field "+ cName, 0, 0);
              return oField;
              }

              var oSub = GetField("subtotal");
              switch(true) {
              case (oSub.value > 500) :
              event.value = 0;
              break;
              case (oSub.value > 250):
              event.value = 25;
              break;
              case (oSub.value > 150):
              event.value = 20;
              break;
              case (oSub.value > 0):
              event.value = 15;
              break;
              default:
              event.value = 0;
              break;
              }

              • 4. Re: Calculate shipping charges using javascript
                zkilmer Level 1

                Thanks y'all.  I got it all straightend out.