diff --git a/mappers/commerce_product_reference.inc b/mappers/commerce_product_reference.inc index 136764b..b4b9bf9 100644 --- a/mappers/commerce_product_reference.inc +++ b/mappers/commerce_product_reference.inc @@ -15,17 +15,21 @@ function _commerce_product_reference_feeds_processor_targets_alter(&$targets, $e $info = field_info_field($name); if ($info['type'] == 'commerce_product_reference') { if (array_key_exists('product_id', $info['columns'])) { + // Real target is modified in order to keep the older values from the + // field of the entity to support accumulative imports. + // See http://drupal.org/node/1188994 + $targets[$name . ":product_id"] = array( 'name' => $instance['label'] . ': Product ID', 'callback' => 'commerce_product_reference_feeds_set_target', 'description' => t('The product id for the @name field. NOTE: use this feature with care, products ids are usually assigned by Drupal.', array('@name' => $instance['label'])), - 'real_target' => $name, + 'real_target' => 'commerce-' . $name, ); $targets[$name . ":sku"] = array( 'name' => $instance['label'] . ': SKU', 'callback' => 'commerce_product_reference_feeds_set_target', 'description' => t('The SKU reference for the @name field. NOTE: the product entity needs to exist.', array('@name' => $instance['label'])), - 'real_target' => $name, + 'real_target' => 'commerce-' . $name, ); } } @@ -39,7 +43,7 @@ function _commerce_product_reference_feeds_processor_targets_alter(&$targets, $e * user has decided to map to and $value contains the value of the feed item * element the user has picked as a source. */ -function commerce_product_reference_feeds_set_target($source, $entity, $target, $value) { +function commerce_product_reference_feeds_set_target(FeedsSource $source, $entity, $target, $value) { if (empty($value)) { return; } @@ -54,22 +58,63 @@ function commerce_product_reference_feeds_set_target($source, $entity, $target, $info = field_info_field($field_name); $field = isset($entity->$field_name) ? $entity->$field_name : array(); - // Iterate over all values. - foreach ($value as $i => $v) { - if (!is_array($v) && !is_object($v)) { - if ($sub_field == 'product_id') { - $field[LANGUAGE_NONE][$i]['product_id'] = $v; - } - elseif ($sub_field == 'sku') { - if ($product = commerce_product_load_by_sku($v)) { - $field[LANGUAGE_NONE][$i]['product_id'] = $product->product_id; - } else { - drupal_set_message(t('A product with SKU %sku could not be found. Please check that the product exists or import it first.', array('%sku' => $v)), 'error'); + // If we're replacing the existing content, we'd want to keep the existing + // field content and update it accordingly. + // Also, if the field is empty, we map the values the usual way. + if ($source->importer->config['processor']['config']['update_existing'] == FEEDS_UPDATE_EXISTING && !empty($field) && count($field[LANGUAGE_NONE])) { + $i = count($field[LANGUAGE_NONE]); + + foreach ($value as $v) { + if (!is_array($v) && !is_object($v)) { + if (strstr($target, 'product_id')) { + $product_id = $v; } + elseif (strstr($target, 'sku')) { + if ($product = commerce_product_load_by_sku($v)) { + $product_id = $product->product_id; + } + else { + drupal_set_message(t('A product with SKU %sku could not be found. Please check that the product exists or import it first.', array('%sku' => $v)), 'error'); + } + } + if ($product_id) { + $delta = $i; + // If there are already some elements in the field, we search for it to reuse them. + foreach($field[LANGUAGE_NONE] as $key=>$id) { + if ($id['product_id'] == $product_id) { + $delta = $key; + break; + } + } + $field[LANGUAGE_NONE][$delta]['product_id'] = $product_id; + } + } + if ($info['cardinality'] == 1) { + break; } + $i++; } - if ($info['cardinality'] == 1) { - break; + } + else { + // If we're not updating, we're replacing or creating so a normal behavior + // is expected. + // Iterate over all values. + foreach ($value as $i => $v) { + if (!is_array($v) && !is_object($v)) { + if ($sub_field == 'product_id') { + $field[LANGUAGE_NONE][$i]['product_id'] = $v; + } + elseif ($sub_field == 'sku') { + if ($product = commerce_product_load_by_sku($v)) { + $field[LANGUAGE_NONE][$i]['product_id'] = $product->product_id; + } else { + drupal_set_message(t('A product with SKU %sku could not be found. Please check that the product exists or import it first.', array('%sku' => $v)), 'error'); + } + } + } + if ($info['cardinality'] == 1) { + break; + } } }