You are here

function commerce_line_items_total in Commerce Core 7

Returns the total price amount and currency of an array of line items.

Parameters

$line_items: The array of line items whose quantities you want to count; also accepts an EntityListWrapper of a line item reference field.

$types: An array of line item types to filter by before totaling.

Return value

An associative array of containing the total 'amount' and 'currency_code' the line items.

See also

commerce_order_calculate_total()

1 call to commerce_line_items_total()
commerce_line_item_handler_area_line_item_summary::render in modules/line_item/includes/views/handlers/commerce_line_item_handler_area_line_item_summary.inc
Render the area.

File

modules/line_item/commerce_line_item.module, line 1509
Defines the core Commerce line item entity and API functions interact with line items on orders.

Code

function commerce_line_items_total($line_items, $types = array()) {

  // First determine the currency to use for the order total. This code has
  // been copied and modifed from commerce_order_calculate_total(). It is worth
  // considering abstracting this code into a separate API function that both
  // functions can use.
  $currency_code = commerce_default_currency();
  $currencies = array();

  // Populate an array of how many line items on the order use each currency.
  foreach ($line_items as $delta => $line_item_wrapper) {

    // Convert the line item to a wrapper if necessary.
    if (!$line_item_wrapper instanceof EntityMetadataWrapper) {
      $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item_wrapper);
    }
    $line_item_currency_code = $line_item_wrapper->commerce_total->currency_code
      ->value();
    if (!array_key_exists($line_item_currency_code, $currencies)) {
      $currencies[$line_item_currency_code] = 1;
    }
    else {
      $currencies[$line_item_currency_code]++;
    }
  }
  reset($currencies);

  // If only one currency is present, use that to calculate the total.
  if (count($currencies) == 1) {
    $currency_code = key($currencies);
  }
  elseif (array_key_exists(commerce_default_currency(), $currencies)) {

    // Otherwise use the site default currency if it's in the array.
    $currency_code = commerce_default_currency();
  }
  elseif (count($currencies) > 1) {

    // Otherwise use the first currency in the array.
    $currency_code = key($currencies);
  }

  // Sum up the total price of all matching line items.
  $total = 0;
  foreach ($line_items as $line_item_wrapper) {

    // Convert the line item to a wrapper if necessary.
    if (!$line_item_wrapper instanceof EntityMetadataWrapper) {
      $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item_wrapper);
    }
    if (empty($types) || in_array($line_item_wrapper
      ->getBundle(), $types)) {
      $total += commerce_currency_convert($line_item_wrapper->commerce_total->amount
        ->value(), $line_item_wrapper->commerce_total->currency_code
        ->value(), $currency_code);
    }
  }
  return array(
    'amount' => $total,
    'currency_code' => $currency_code,
  );
}