Changes

Multiple product calculation

3,495 bytes added, 14:24, 20 June 2012
JavaScript code to calculate the total cost of different combination of products from a list of multiple drop down lists

== Description ==

This demo will use the example of two newspapers with different prices based on the day of the week.
If a sales rep goes to the client the client will be able to select the newspaper issues that he/she wants. The code will also calculate the weekly cost of the combination
of the issues that the client wants to purchase.


== Create the Variables ==

''var tempCost = 0;''

''var int_times=new Array();''

''int_times[0]=0;''

''int_times[1]=0;''

''int_times[2]=0;''

''int_times[3]=0;''

''int_times[4]=0;''

''int_times[5]=0;''

''var int_star=new Array();''

''int_star[0]=0;''

''int_star[1]=0;''

''int_star[2]=0;''

''int_star[3]=0;''

''int_star[4]=0;''

''int_star[5]=0;''

----
This creates two arrays for the two newspapers with the ability to store the price for six days
e.g. if the client want the Times only on a Monday, the array will look like this

int_times[0]=1.90; And the rest of the array will be all set to zero


== Getting the selected values from the Drop down lists ==


''var strCDA_X_field_0_X = document.getElementById('strCDA_X_field_0_X ');//Times mon''

''var strCDA_X_field_0_X = document.getElementById('strCDA_X_field_0_X ');//Times tue''

''var strCDA_X_field_0_X = document.getElementById('strCDA_X_field_0_X ');//Times wed''

''var strCDA_X_field_0_X = document.getElementById('strCDA_X_field_0_X ');//Times thur''

''var strCDA_X_field_0_X = document.getElementById('strCDA_X_field_0_X ');//Times fri''

''var strCDA_X_field_0_X = document.getElementById('strCDA_X_field_0_X ');//Times sat''

''var strCDA_X_field_0_X = document.getElementById('strCDA_X_field_0_X ');//Star mon''

''var strCDA_X_field_0_X = document.getElementById('strCDA_X_field_0_X ');//Star tue''

''var strCDA_X_field_0_X = document.getElementById('strCDA_X_field_0_X ');//Star wed''

''var strCDA_X_field_0_X = document.getElementById('strCDA_X_field_0_X ');//Star thur''

''var strCDA_X_field_0_X = document.getElementById('strCDA_X_field_0_X ');//Star fri''

''var strCDA_X_field_0_X = document.getElementById('strCDA_X_field_0_X ');//Star sat''


----
This is getting the values that are associated with the the drop down lists in the campaign
To adapt this to a new campaign where it says 'strCDA_X_field_0_X ' simply swap the 'X' for the numbers associated fields in your campaign



== Calculating the Cost of the combinations of the products selected ==



''var temp_Times=int_times[0]+int_times[1]+int_times[2]+int_times[3]+int_times[4]+int_times[5];''

Adds up the values of the selected issues that the client wants for the Times newspaper
----


''var temp_Star=int_star[0]+int_star[1]+int_star[2]+int_star[3]+int_star[4]+int_star[5];''

Adds up the values of the selected issues that the client wants for the Star newspaper
----

''tempCost=temp_Times+temp_Star;''

Combines the value of the times and the star to get a total cost'''
----

''tempCost=tempCost.toFixed(2);''

This line rounds the value to two decimal places to avoid a total value of 1.569541 and rounds it to 1.57
----

document.getElementById('strCDA_X_field_0_X').value=tempCost;''

This line then adds the total amount to the pre-selected field that you want the total price to be inserted into.
To adapt this to a new campaign where it says 'strCDA_X_field_0_X ' simply swap the 'X' for the numbers associated fields in your campaign
----