You are here

public function MigrateCommercePriceFieldHandler::prepare in Commerce Migrate 7

Prepare field.

Parameters

object $entity: Entity object.

array $field_info: Field definition.

array $instance: Field instance.

array $values: Values of the field.

Return value

array Drupal representation of a field.

File

plugins/destinations/fields.inc, line 89
Support for processing commerce fields (product reference, customer profile reference, price)

Class

MigrateCommercePriceFieldHandler
Class MigrateCommercePriceFieldHandler.

Code

public function prepare($entity, array $field_info, array $instance, array $values) {
  $arguments = isset($values['arguments']) ? $values['arguments'] : array();
  unset($values['arguments']);
  $currency_code = empty($arguments['currency_code']) ? commerce_default_currency() : $arguments['currency_code'];
  $components = empty($arguments['components']['arguments']) ? array() : array_filter($arguments['components']['arguments'], 'is_numeric');

  // Detect field language.
  $language = $this
    ->getFieldLanguage($entity, $field_info, $arguments);

  // Initial price should not contain "amount" - it will be added later.
  $price = array(
    'currency_code' => $currency_code,
  );

  // If no price components going to be set - add "base_price".
  if (empty($components)) {
    $components['base_price'] = $values[0];
  }
  foreach ($components as $name => $component) {

    // Allow to component be a scalar amount value.
    if (!is_array($component)) {
      $component = array(
        'amount' => $component,
      );
    }

    // Extend component by default properties.
    $component += array(
      'included' => TRUE,
      'currency_code' => $currency_code,
    );

    // The "included" property - not a part of components array.
    $included = (bool) $component['included'];
    unset($component['included']);

    // Calculate decimal amount.
    $component['amount'] = commerce_currency_decimal_to_amount($component['amount'], $component['currency_code']);

    // Add a component.
    $price['data'] = commerce_price_component_add($price, $name, $component, $included, FALSE);
  }

  // Calculate total amount of all of the components.
  $total = commerce_price_component_total($price);

  // Copy price components.
  $total['data'] = $price['data'];
  if (isset($arguments['tax_rate'])) {
    $total['data']['include_tax'] = $arguments['tax_rate'];
  }
  return array(
    $language => array(
      $total,
    ),
  );
}