You are here

function commerce_price_feeds_set_target in Commerce Feeds 7

Callback for mapping. Here is where the actual mapping happens.

When the callback is invoked, $target contains the name of the field the user has decided to map to and $value contains the value of the feed item element the user has picked as a source.

1 string reference to 'commerce_price_feeds_set_target'
_commerce_price_feeds_processor_targets_alter in mappers/commerce_price.inc
Implements hook_feeds_processor_targets_alter().

File

mappers/commerce_price.inc, line 40
On behalf implementation of Feeds mapping API for commerce_price.module.

Code

function commerce_price_feeds_set_target($source, $entity, $target, $value, array $mapping) {
  if (!isset($value)) {
    return;
  }

  // Handle non-multiple value fields. Map empty string to empty array.
  if (!is_array($value)) {
    $value = trim($value) === '' ? array() : array(
      $value,
    );
  }

  // Get field information.
  list($field_name, $sub_field) = explode(':', $target);
  $info = field_info_field($field_name);
  $field = array();
  $config = $source->importer
    ->getConfig();
  $entity_type = $source->importer->processor
    ->entityType();
  $language = $mapping['language'];

  // Get the default currency from the field instance. Fallback to the default.
  $currency = commerce_default_currency();
  if ($sub_field == 'amount') {
    $info_instance = field_info_instance($entity_type, $field_name, $entity->type);
    if (!isset($info_instance['widget']['settings']['currency_code']) || $info_instance['widget']['settings']['currency_code'] != 'default') {
      $currency = $info_instance['widget']['settings']['currency_code'];
    }
  }

  // Iterate over all values.
  foreach ($value as $i => $v) {
    if (!is_array($v) && !is_object($v)) {
      if ($sub_field == 'amount') {
        $field[$language][$i]['amount'] = $v;

        // If the currency code is not set yet by other mapping, set the
        // default one.
        if (!isset($field[$language][$i]['currency_code'])) {
          $field[$language][$i]['currency_code'] = $currency;
        }
        if (module_exists('commerce_tax')) {
          if (!empty($config['processor']['config']['tax_rate'])) {
            $field[$language][$i]['data']['include_tax'] = $config['processor']['config']['tax_rate'];
          }
        }
      }
      elseif ($sub_field == 'currency_code') {
        $field[$language][$i]['currency_code'] = $v;
      }
    }
    if ($info['cardinality'] == 1) {
      break;
    }
  }
  $entity->{$field_name} = $field;
}