Cost calculation functions (For Loop)
If you have complex cost calculations, you might find it easier to define each one in a separate function that is then referenced in a <name>CostLineItem variable in the estimateCost(order)function. You might consider using a separate function if your cost calculation involves a For Loop.
For Loops are handy if you modify the number of Binding options available, or add/remove custom fields on a regular basis. Using a For Loop means you do not need to know the exact position of a sub-attribute within the configuration. You can simply traverse the Binding options available (for example) and match on it. If you don't use a For Loop, you will need to update your cost script every time you change a sub-attribute in the product configuration file.
The structure of this function is as follows:
- Define the function and pass in the order object
- Declare the variables
- Define the variables
- Define the cost calculation value
- Return the a value to the calling function
This example shows a function that checks the value assigned to each selected sub-attribute option (for example, the selected color), for each sub-attribute (for example, Color and Coil thickness) under the Coil Binding option.