You are here

uc_feeds.module in Ubercart Feed Mappers 6

Same filename and directory in other branches
  1. 7 uc_feeds.module

Integrates Ubercart properties with Feeds

File

uc_feeds.module
View source
<?php

/**
 * @file
 * Integrates Ubercart properties with Feeds
 */

/**
 * Implementation of hook_feeds_node_processor_targets_alter().
 * @param array $targets
 * @param string $content_type
 * @return void
 */
function uc_feeds_feeds_node_processor_targets_alter(&$targets, $content_type) {

  // Proceed only if the content_type is a product type.
  if (in_array($content_type, uc_product_types())) {

    // Model
    $targets['uc_product:model'] = array(
      'name' => t('UC: Model/SKU'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('Model/SKU'),
      )),
    );

    // List price
    $targets['uc_product:list_price'] = array(
      'name' => t('UC: List price'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('List price'),
      )),
    );

    // Cost
    $targets['uc_product:cost'] = array(
      'name' => t('UC: Cost'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('Cost'),
      )),
    );

    // Sell price
    $targets['uc_product:sell_price'] = array(
      'name' => t('UC: Sell price'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('Sell price'),
      )),
    );

    // Weight
    $targets['uc_product:weight'] = array(
      'name' => t('UC: Weight'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('Weight'),
      )),
    );
    $targets['uc_product:weight_units'] = array(
      'name' => t('UC: Weight Unit'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('Weight Unit'),
      )),
    );

    // Dims
    $targets['uc_product:length'] = array(
      'name' => t('UC: Length'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('Length'),
      )),
    );
    $targets['uc_product:width'] = array(
      'name' => t('UC: Width'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('Width'),
      )),
    );
    $targets['uc_product:height'] = array(
      'name' => t('UC: Height'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('Height'),
      )),
    );
    $targets['uc_product:length_units'] = array(
      'name' => t('UC: Dimension Units'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('Dimension Units'),
      )),
    );
    $targets['uc_product:pkg_qty'] = array(
      'name' => t('UC: pkg_qty'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('Package Quantity'),
      )),
    );
    $targets['uc_product:default_qty'] = array(
      'name' => t('UC: default_qty'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('Default Quantity to add to cart'),
      )),
    );

    // Shippable
    $targets['uc_product:shippable'] = array(
      'name' => t('UC: Shippable'),
      'callback' => 'uc_feeds_feeds_set_target',
      'description' => t('Ubercart: !mapper', array(
        '!mapper' => t('Shippable'),
      )),
    );

    // Stock
    if (module_exists('uc_stock')) {
      $targets['uc_stock:stock'] = array(
        'name' => t('UC: Stock Level'),
        'callback' => 'uc_feeds_feeds_set_target',
        'description' => t('Ubercart: !mapper', array(
          '!mapper' => t('Stock'),
        )),
      );
    }

    // TODO : Price By Role, others?
    // Need new patches
    // Attributes
    if (module_exists('uc_attribute') && function_exists('uc_attribute_load_multiple')) {
      $attribs = uc_attribute_load_multiple();
      foreach ($attribs as $attrib) {
        $aid = $attrib->aid;
        foreach ($attrib->options as $option) {
          $oid = $option->oid;
          $targets['uc_attribute:attribute_price_' . $aid . '_' . $oid] = array(
            'name' => t('UCA Price: @attribute:@option', array(
              '@attribute' => $attrib->name,
              '@option' => $option->name,
            )),
            'callback' => 'uc_feeds_feeds_set_target',
            'description' => t('Ubercart: !mapper', array(
              '!mapper' => t('Attribute Price: @attribute:@option', array(
                '@attribute' => $attrib->name,
                '@option' => $option->name,
              )),
            )),
          );
        }
      }
      foreach ($attribs as $attrib) {
        $aid = $attrib->aid;
        foreach ($attrib->options as $option) {
          $oid = $option->oid;
          $targets['uc_attribute:attribute_weight_' . $aid . '_' . $oid] = array(
            'name' => t('UCA Weight: @attribute:@option', array(
              '@attribute' => $attrib->name,
              '@option' => $option->name,
            )),
            'callback' => 'uc_feeds_feeds_set_target',
            'description' => t('Ubercart: !mapper', array(
              '!mapper' => t('Attribute Weight: @attribute:@option', array(
                '@attribute' => $attrib->name,
                '@option' => $option->name,
              )),
            )),
          );
        }
      }
    }
  }
}

/**
 * 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.
 *
 * @param object $node
 * @param string $target
 * @param mixed $value
 * @return void
 */
function uc_feeds_feeds_set_target(&$node, $target, $value) {
  if (is_array($value) || $value == '') {
    return;
  }

  // Examine target: first part is module, second part is target
  list($module, $target) = explode(':', $target);
  switch ($module) {

    // Ubercart Attributes
    case 'uc_attribute':
      $ao_arr = explode('_', $target);
      $aid = $ao_arr[2];
      $oid = $ao_arr[3];

      // just flag the attributes for now - node API will take care of saving them
      $node->uc_feeds_flag = 'Attributes';
      if (substr($target, 0, 15) == 'attribute_price') {
        $node->attributes[$aid]->options[$oid]->price = $value;
      }
      elseif (substr($target, 0, 16) == 'attribute_weight') {
        $node->attributes[$aid]->options[$oid]->weight = $value;
      }
      return;

    // Stock
    case 'uc_stock':
      if (!isset($node->model)) {

        // Error: model is unknown.
        drupal_set_message(t("You can not set the stock for a product if the Model/SKU is not known. Ensure that you map the Model/SKU first.", 'warning'));
        return;
      }
      if (uc_stock_is_active($node->model)) {

        // Update stock levels
        uc_stock_set($node->model, $value);
      }
      else {

        // Insert new stock level. Not sure how to set the threshold. This would be zero now.
        $sQuery = "INSERT INTO {uc_product_stock} (sku, nid, active, stock) VALUES ('%s', %d, %d, %d)";
        db_query($sQuery, $node->model, $node->nid, 1, $value);
      }
      return;

    // Product
    case 'uc_product':
      switch ($target) {
        case 'length':
        case 'width':
        case 'height':
          $d = "dim_{$target}";
          $node->{$d} = $value;
          break;
        default:
          $node->{$target} = $value;
      }
      return;
  }
}

/**
 * Implementation of hook_nodeapi().
 * @param object $node
 * @param string $op
 * @return void
 */
function uc_feeds_nodeapi(&$node, $op) {
  if (!$node->uc_feeds_flag == 'Attributes') {
    return;
  }
  switch ($op) {
    case 'insert':
      if (module_exists('uc_attribute')) {
        foreach ($node->attributes as $aid => $feedattrib) {

          // Enable all options for added attributes.
          $attribute = uc_attribute_load($aid);
          foreach ($attribute->options as $option) {
            if ($node->attributes[$aid]->options[$option->oid]) {
              $option->price = $node->attributes[$aid]->options[$option->oid]->price;
              $option->weight = $node->attributes[$aid]->options[$option->oid]->weight;
            }
            db_query("INSERT INTO {uc_product_options} (nid, oid, cost, price, weight, ordering) VALUES (%d, %d, %f, %f, %f, %d)", $node->nid, $option->oid, $option->cost, $option->price, $option->weight, $option->ordering);
          }

          // Make the first option (if any) the default.
          $option = reset($attribute->options);
          if ($option) {
            $oid = $option->oid;
          }
          else {
            $oid = 0;
          }
          db_query("INSERT INTO {uc_product_attributes} (nid, aid, label, ordering, default_option, required, display) SELECT %d, aid, label, ordering, %d, required, display FROM {uc_attributes} WHERE aid = %d", $node->nid, $oid, $aid);
        }
      }
      break;
    case 'update':
      uc_stock_adjust($node->model, $node->stock);
      break;
  }
}

Functions

Namesort descending Description
uc_feeds_feeds_node_processor_targets_alter Implementation of hook_feeds_node_processor_targets_alter().
uc_feeds_feeds_set_target Callback for mapping. Here is where the actual mapping happens.
uc_feeds_nodeapi Implementation of hook_nodeapi().