You are here

function commerce_price_component_total in Commerce Core 7

Returns the total value of components in a price array converted to the currency of the price array.

Parameters

$price: The price whose components should be totalled.

$name: Optionally specify a component name to restrict the totalling to components of that type.

Return value

A price array representing the total value.

2 calls to commerce_price_component_total()
commerce_order_calculate_total in modules/order/commerce_order.module
Calculates the order total, updating the commerce_order_total field data in the order object this function receives.
commerce_price_field_formatter_view in modules/price/commerce_price.module
Implements hook_field_formatter_view().

File

modules/price/commerce_price.module, line 1082
Defines the Price field with widgets and formatters used to add prices with currency codes to various Commerce entities.

Code

function commerce_price_component_total($price, $name = NULL) {

  // Initialize the total price array.
  $total = array(
    'amount' => 0,
    'currency_code' => $price['currency_code'],
    'data' => array(),
  );

  // Bail out if there are no components.
  if (empty($price['data']['components'])) {
    return $total;
  }

  // Loop over each component.
  foreach ($price['data']['components'] as $key => $component) {

    // If we're totalling all components or this one matches the requested type...
    if (empty($name) || $name == $component['name']) {
      $total['amount'] += commerce_currency_convert($component['price']['amount'], $component['price']['currency_code'], $total['currency_code']);
    }
  }
  return $total;
}