You are here

function commerce_line_item_unit_price_currency_convert in Commerce Core 7

Rules action: convert the unit price to a different currency.

File

modules/line_item/commerce_line_item.rules.inc, line 307
Rules integration for line items.

Code

function commerce_line_item_unit_price_currency_convert($line_item, $currency_code) {
  $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price');

  // Only convert non-NULL amounts since NULL amounts do not have a currency.
  if (empty($unit_price)) {
    return;
  }
  $wrapper->commerce_unit_price->amount = commerce_currency_convert($wrapper->commerce_unit_price->amount
    ->value(), $wrapper->commerce_unit_price->currency_code
    ->value(), $currency_code);
  $wrapper->commerce_unit_price->currency_code = $currency_code;

  // Convert the currency code of the price's components.
  if (!empty($unit_price['data']['components'])) {
    foreach ($unit_price['data']['components'] as $key => &$component) {
      $component['price']['amount'] = commerce_currency_convert($component['price']['amount'], $component['price']['currency_code'], $currency_code);
      $component['price']['currency_code'] = $currency_code;
    }
    $wrapper->commerce_unit_price->data = $unit_price['data'];
  }
}