Skip to main content
Participant
June 27, 2022
Answered

Help Needed for Custom Javascript Calculations Product Order Form

  • June 27, 2022
  • 1 reply
  • 2091 views

Trying to get a total sum based on a particular variable in the backend of my javascript calculations on an product order form.

 

So on a product order form, there's a text field for quantity (no. of panels) and a checkbox (with the base price added to it).

 

When the checkbox is selected, the standard base price is added in the format properties (£300) and appears in one of the 'total' textfields. However the part I'm having difficulty with is having the quantity number determining what the total sum is based on a specific additional price(s). 

 

Example: The aluminium base is £300.00 when checked. When the quantity for no. of panels is set to 2, the number should remain the same at £300. When the quantity is 3 panels, this needs to add an additional £40.00, totalling £340.00. If 4 is entered for the number of panels, that should add another £80.00 on top of it (total being £380.00), if 5 is typed into the quantity box, £120.00 needs to be added to the £300, totalling £420.00) and so on. 

 

Aluminim base checkbox is "AluBase" while the quantity textfield is "NoPanels".

 

Now how do I create the correct javascript custom calculation/simplified field notion calculation to achieve this?

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

As custom calculation script where you want to calculate total use this:

var v1 = Number(this.getField("NoPanels").valueAsString);
var check = this.getField("AluBase").valueAsString;
var cPrice = "";
var str = [
{qty: 1, Price:300},
{qty: 2, Price:300},
{qty: 3, Price:340},
{qty: 4, Price:380},
{qty: 5, Price:420}];
for(var i in str){
if(v1 == str[i].qty && check != "Off")
cPrice = str[i].Price;}
event.value = cPrice;

 

You can easily add more quantity and prices.

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
June 27, 2022

As custom calculation script where you want to calculate total use this:

var v1 = Number(this.getField("NoPanels").valueAsString);
var check = this.getField("AluBase").valueAsString;
var cPrice = "";
var str = [
{qty: 1, Price:300},
{qty: 2, Price:300},
{qty: 3, Price:340},
{qty: 4, Price:380},
{qty: 5, Price:420}];
for(var i in str){
if(v1 == str[i].qty && check != "Off")
cPrice = str[i].Price;}
event.value = cPrice;

 

You can easily add more quantity and prices.

Participant
June 27, 2022

Amazing, this has worked perfectly, especially where I needed to edit the price. Thank you so much!