How to use Feeds Importer with Commerce Price Table

Submitted by Brandon Cone on 04/30/2013 - 10:00:am

If you have an ecommerce store, chances are you have some need for bulk pricing rules for customers when purchasing large amounts of items.  Commerce Price Table provides configurable bulk pricing per product and gets you a long way down this road.

However, if you are setting up a new site or importing products into a store using feeds_commerce, chances are you need to be able to import your bulk pricing setup.  In order to accomplish this, you will need to create a custom module to implement the processor for commerce_price_table.

1. Implement hook_feeds_processor_targets_alter()

  1. /**
  2.  * Implements hook_feeds_processor_targets_alter().
  3.  *
  4.  * Needed for importing Price Table data
  5.  */
  6. function my_module_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  7.   foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  8.     $info = field_info_field($name);
  9.     if ($info['type'] == 'commerce_price_table') {
  10.       $targets[$name] = array(
  11.         'name' => $instance['label'],
  12.         'callback' => 'my_module_feeds_set_target_price_table',
  13.         'description' => t('The table price for @name field.', array('@name' => $instance['label'])),
  14.         'real_target' => $name,
  15.       );
  16.     }
  17.   }
  18. }

2. Implement the processor callback.

  1. /**
  2.  * Callback for mapping. Here is where the actual mapping happens.
  3.  *
  4.  * When the callback is invoked, $target contains the name of the field the
  5.  * user has decided to map to and $value contains the value of the feed item
  6.  * element the user has picked as a source.
  7.  *
  8.  * Needed for importing Price Table data
  9.  */
  10. function cf_custom_feeds_set_target_price_table($source, $entity, $target, $value, $mapping) {
  11.  
  12.   if (empty($value)) {
  13.     return;
  14.   }
  15.   // Handle non-multiple value fields.
  16.   if (!is_array($value)) {
  17.     $value = array($value);
  18.   }
  19.   // Iterate over all values.
  20.   $info = field_info_field($target);
  21.   $field = isset($entity->$target) ? $entity->$target : array();
  22.   $i = isset($field[LANGUAGE_NONE]) ? count($field[LANGUAGE_NONE]) : 0;
  23.   foreach ($value as $v) {
  24.     if (!is_array($v) && !is_object($v)) {
  25.       list($amount, $min_qty, $max_qty) = explode('|', $v);
  26.       // always put the default currency
  27.       $currency_code = commerce_default_currency();
  28.       $field[LANGUAGE_NONE][$i]['currency_code'] = $currency_code;
  29.       $field[LANGUAGE_NONE][$i]['amount'] = commerce_currency_amount_to_decimal($amount, $currency_code);
  30.       $field[LANGUAGE_NONE][$i]['min_qty'] = $min_qty;
  31.       $field[LANGUAGE_NONE][$i]['max_qty'] = $max_qty;
  32.       $i++;
  33.     }
  34.     if ($info['cardinality'] == 1) {
  35.       break;
  36.     }
  37.   }
  38.   $entity->{$target} = $field;
  39. }

3. Set up your importer to format the data appropriately:

How you do this will be dependent on the format of your import source.  What it comes down to is that you need your price table entries to look something like this: 40.00|0|10;30.00|11|25;20.00|25|50;10.00|51|-1 where each entry is separated by a ; and the price, minimum quantity, and maximum quantity are separated by a |.

There is a posting in the issue queue for Commerce Price Table about this here.