Help Center menu


Job Ticketing manual


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.

Define the function and pass in the order object

function calculateBindingCost(order) {

Declare the variables

  var items = []; 
  var total = 0; 
  var numberOfSubAttributes = 0; 
  var coilBindingCost = 0; 
  var coilThicknessCost = 0; 
  var coilBindingColorCost = 0;

Define the variables

  // Check if an option with sub-attributes was selected 
  // (e.g. Coil Binding) 
  if (order.binding.attributes) {

    // Obtain the number of sub-attributes so we can loop through 
    // to check the value of each selected option (for coil binding,  
    // we would loop through: coil thickness + color)
      numberOfSubAttributes = order.binding.attributes.length;

        // If the sub-attribute has one or more option,  
        // check each option value sequentially
          if (numberOfSubAttributes > 0) {
            // Loop through
            for (var i = 0; i < numberOfSubAttributes; i++) {
                // Grab the cost of the selected coil thickness option 
                // (e.g. cost of thick coil, medium coil, or thin coil)
                if (order.binding.attributes[i].name == "Coil thickness") {
                    coilThicknessCost = order.binding.attributes

                // Grab the cost of the selected color option
                // (e.g. cost of blue, red, navy)
                if (order.binding.attributes[i].name == "Color") {
                    coilBindingColorCost = order.binding.attributes

Define the cost calculation value

  coilBindingCost = coilThicknessCost + coilBindingColorCost;

Return a value to the calling function

  return coilBindingCost; }