Need help with cron hook to update uc_stock

Events happening in the community are now at Drupal community events on www.drupal.org.
Anonymous's picture

Can someone please write me a cron hook for Drupal 7 Ubercart 3 to update uc_stock?
I'm using feeds and uc_feeds to import product data. All I need is to update the existing product stock field.
I've included uc_feeds for D7 and the uc_feeds-6.x-1.x that uses the stock update. The stock update is not written in the D7 version. After many, many attempts, I just can't write it myself.

Thanks,

Katie

Here's uc_feeds-7.x:
<?php

/**
* Implementation of hook_feeds_processor_targets_alter().
*/
function uc_feeds_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
// Proceed only if the content_type is a product type.
if ($entity_type == 'node' && in_array($bundle_name, uc_product_types())) {
// Model
$targets['model'] = array(
'name' => t('UC: Model/SKU'),
'callback' => 'uc_feeds_set_target',
'description' => 'Ubercart:' . t('Model/SKU'),
);
// List price
$targets['list_price'] = array(
'name' => t('UC: List price'),
'callback' => 'uc_feeds_set_target',
'description' => 'Ubercart:' . t('List price'),
);
// Cost
$targets['cost'] = array(
'name' => t('UC: Cost'),
'callback' => 'uc_feeds_set_target',
'description' => 'Ubercart:' . t('Cost'),
);
// Sell price
$targets['sell_price'] = array(
'name' => t('UC: Sell price'),
'callback' => 'uc_feeds_set_target',
'description' => 'Ubercart:' . t('Sell price'),
);
// Weight
$targets['weight'] = array(
'name' => t('UC: Weight'),
'callback' => 'uc_feeds_set_target',
'description' => 'Ubercart:' . t('Weight'),
);
$targets['weight_units'] = array(
'name' => t('UC: Weight Unit'),
'callback' => 'uc_feeds_set_target',
'description' => 'Ubercart:' . t('Weight Unit'),
);

// Attributes
if (module_exists("uc_attribute")) {
  $attribs = uc_attribute_load_multiple();

  foreach ($attribs as $attrib) {
    $aid = $attrib->aid;

    foreach ($attrib->options as $option) {
      $oid = $option->oid;

      // Attribute Price
      $targets['attribute_price_' . $aid . "_" . $oid] = array(
        'name' => t('UCA Price: ' . $attrib->name . ":" . $option->name),
        'callback' => 'uc_feeds_set_target',
        'description' => 'Ubercart:' . t('Attribute Price:' . $attrib->name . ":" . $option->name),
      );

      // Attribute Cost
      $targets['attribute_cost_' . $aid . "_" . $oid] = array(
        'name' => t('UCA Cost: ' . $attrib->name . ":" . $option->name),
        'callback' => 'uc_feeds_set_target',
        'description' => 'Ubercart:' . t('Attribute Cost:' . $attrib->name . ":" . $option->name),
      );

      // Attribute Weight
      $targets['attribute_weight_' . $aid . "_" . $oid] = array(
        'name' => t('UCA Weight: ' . $attrib->name . ":" . $option->name),
        'callback' => 'uc_feeds_set_target',
        'description' => 'Ubercart:' . t('Attribute Weight:' . $attrib->name . ":" . $option->name),
      );
    }
  }
}

}
}

/**
* user has decided to map to and $value contains the value of the feed item
* element the user has picked as a source.
*/
function uc_feeds_set_target($source, $node, $target, $value) {
if (!is_array($value)) {
if (substr($target, 0, 10) != "attribute_") {
$node->$target = $value;
}
else {
$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 value is empty, it's not an attribute we want to use
  if (substr($target, 0, 15) == "attribute_price" && $value != '') {
    $node->attributes[$aid]->options[$oid]->price = $value;
  }
  elseif (substr($target, 0, 14) == "attribute_cost" && $value != '') {
    $node->attributes[$aid]->options[$oid]->cost = $value;
  }
  elseif (substr($target, 0, 16) == "attribute_weight" && $value != '') {
    $node->attributes[$aid]->options[$oid]->weight = $value;
  }
}

}
}

/**
* Implements hook_feeds_node_insert().
*/
function uc_feeds_node_insert($node) {
if (uc_product_is_product($node) && module_exists("uc_attribute")) {
foreach ($node->attributes as $aid => $feedattrib) {
// Load all options for added attributes. (Will only enable the ones set in the import)
$attribute = uc_attribute_load($aid);

  foreach ($attribute->options as $option) {
    // some part of the option is set, default all empty parts to 0 or ''
    if (isset($node->attributes[$aid]->options[$option->oid])) {
      $option->price = ($node->attributes[$aid]->options[$option->oid]->price != '') ? $node->attributes[$aid]->options[$option->oid]->price : 0;
      $option->cost = ($node->attributes[$aid]->options[$option->oid]->cost != '') ? $node->attributes[$aid]->options[$option->oid]->cost : 0;
      $option->weight = ($node->attributes[$aid]->options[$option->oid]->weight != '') ? $node->attributes[$aid]->options[$option->oid]->weight : 0;

      $id = db_insert('uc_product_options')->fields(array(
        'nid' => $node->nid,
        'oid' => $option->oid,
        'cost' => $option->cost,
        'price' => $option->price,
        'weight' => $option->weight,
        'ordering' => $option->ordering,
      ))->execute();
    }
  }

  // Make the first option (if any) the default.
  $option = reset($attribute->options);
  if ($option) {
    $oid = $option->oid;
  }
  else {
    $oid = 0;
  }
  // TODO Please convert this statement to the D7 database API syntax.

  //Drupal 7 piece added
  $res = db_select('uc_attributes', 'c')
  ->fields('c')
  ->condition('aid',$aid)
  ->execute()
  ->fetchAssoc();

  $nid = db_insert('uc_product_attributes') // Table name no longer needs {}
  ->fields(array(
      'nid' => $node->nid,
      'aid' => $res['aid'],
      'label' => $res['label'],
      'ordering' => $res['ordering'],
      'default_option' => $oid,
      'required' => $res['required'],
      'display' => $res['display'],
    ))
  ->execute();
}

}
}

uc_feeds-6.x-1.x

<?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?

// 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;
}
}

Comments

Possible solution

Hammad.Chishti's picture

Hello,

My problem was same like you and i have done with my own query to update all products stock by putting the products SKU,nid,stock quantity,Threshold in the table uc_product_stock.
I am not sure about the hook but you can do it by your own module and hit your function in evry cron to get all produts and update it. Its working fine on my site.

Thanks

Boise

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: