You are here

function commerce_multicurrency_commerce_price_field_formatter_prepare_view in Commerce Multicurrency 7

Implements hook_commerce_price_field_formatter_prepare_view().

Allows to configure which fields are send to the rules processing.

See also

commerce_product_pricing_commerce_price_field_formatter_prepare_view()

File

./commerce_multicurrency.module, line 540
Enhancements for the commerce currency support.

Code

function commerce_multicurrency_commerce_price_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {

  // If this is a single value purchase price field attached to a product...
  if ($entity_type == 'commerce_product' && $field['module'] == 'commerce_price' && $field['cardinality'] == 1 && $field['field_name'] != 'commerce_price') {

    // Prepare the items for each entity passed in.
    foreach ($entities as $product_id => $product) {

      // If this price should be converted...
      if (!empty($displays[$product_id]['settings']['calculation']) && $displays[$product_id]['settings']['calculation'] == 'currency_specific_price') {

        // If this price has already been converted, reset it to its original
        // value so it can be re-converted afresh in the current context.
        if (isset($items[$product_id][0]['original'])) {
          $original = $items[$product_id][0]['original'];
          $items[$product_id] = array(
            0 => $original,
          );

          // Reset the price field value on the product object used to perform
          // the conversion.
          foreach ($product->{$field['field_name']} as $langcode => $value) {
            $product->{$field['field_name']}[$langcode] = $items[$product_id];
          }
        }
        else {

          // Save the original value for use in subsequent conversions.
          $original = isset($items[$product_id][0]) ? $items[$product_id][0] : NULL;
        }

        // First create a pseudo product line item that we will pass to Rules.
        $line_item = commerce_product_line_item_new($product);

        // Overwrite the unit price by the value of the current field.
        $line_item->commerce_unit_price = $product->{$field['field_name']};

        // If the current field doesn't contain a value - fake an empty one.
        $field_lang = field_language('commerce_line_item', $line_item, 'commerce_unit_price');
        if (empty($line_item->commerce_unit_price)) {
          $line_item->commerce_unit_price = array(
            $field_lang => array(
              array(
                'amount' => 0,
                'currency_code' => commerce_default_currency(),
                'data' => array(),
              ),
            ),
          );
        }
        if (!empty($line_item->commerce_unit_price[$field_lang][0])) {

          // Add the base price to the components array.
          if (!commerce_price_component_load($line_item->commerce_unit_price[$field_lang][0], 'base_price')) {
            $line_item->commerce_unit_price[$field_lang][0]['data'] = commerce_price_component_add($line_item->commerce_unit_price[$field_lang][0], 'base_price', $line_item->commerce_unit_price[$field_lang][0], TRUE);
          }
        }

        // Fire the rules event to handle the display currency.
        rules_invoke_event('commerce_multicurrency_set_display_price', $line_item, $field['field_name']);

        // Replace the data being displayed with data from the converted price.
        $items[$product_id] = array();
        $items[$product_id][0] = entity_metadata_wrapper('commerce_line_item', $line_item)->commerce_unit_price
          ->value();
        $items[$product_id][0]['original'] = $original;
      }
    }
  }
}