You are here

function commerce_webform_calculate_total_default_price_of_webform in Commerce Webform 7

Same name and namespace in other branches
  1. 8 commerce_webform.module \commerce_webform_calculate_total_default_price_of_webform()
  2. 7.2 commerce_webform.module \commerce_webform_calculate_total_default_price_of_webform()

Calculate the total default price of a webform. This is the sum of all productfields if they have a default set.

Parameters

object $node: The webform node

Return value

int The price in minor units.

File

./commerce_webform.module, line 682
Commerce Webform module file

Code

function commerce_webform_calculate_total_default_price_of_webform($node) {
  if (!is_object($node)) {
    $node = node_load($node);
  }
  $total = 0;
  $components = $node->webform['components'];

  // Loop through all possible webform fields looking for
  // productfields.
  foreach ($components as $key => $component) {
    if ($component['type'] == 'productfield') {

      // Loop through the product options on the product field
      // looking for the default product.
      if ($component['value']) {
        $skus = explode(',', $component['value']);
        foreach ($skus as $sku) {
          $product = commerce_product_load_by_sku(trim($sku));
          if ($product) {
            $price = commerce_product_calculate_sell_price($product);
            $total += $price['amount'];
          }
        }
      }
    }
  }
  return $total;
}