0

I want to call uc_stock module by reference in my custom module. Actually I want to add and delete stocks. I have done this first using node_presave and it works successfully, here is the code:


function my_module_node_presave($node){
  if($node->type == 'add_stock') {
    //drupal_set_message('stock_save :'.print_r($node,true));
    //drupal_set_message($node->field_reference['und'][0]['nid']);
    //$nid=16;
    //$p_node = node_load($nid);
    //drupal_set_message($p_node->field_product_stock['und'][0]['value']);
    $product = $node->field_stock_value['und'][0]['value'];
    drupal_set_message('product :'.$product);
    $nid = $node->field_reference['und'][0]['nid'];
    drupal_set_message('nid :'.$nid);
    $p_node = node_load($nid);
    $stock = $p_node->field_product_stock['und'][0]['value'];
    drupal_set_message('stock :'.$stock);

    $stock = $product + $stock;
    drupal_set_message('Stock :'.$stock);
    $p_node->field_product_stock['und'][0]['value'] = $stock;
    //$p_node = node_load($nid);

    //drupal_set_message($p_node);
   // drupal_set_message('node_load :'.print_r($p_node,true));

    node_save($p_node);
    //drupal_set_message('after saving product');
  }
 elseif($node->type == 'sold_stock') {
   $new_product = $node->field_sold_stock['und'][0]['value'];
   drupal_set_message('new product :'.$new_product);
  $new_nid = $node->field_stock_reference['und'][0]['nid'];
   drupal_set_message('new nid :'.$new_nid);
   $new_node = node_load($new_nid);
  $sold_product = $new_node->field_product_stock['und'][0]['value'];
   drupal_set_message('sold product :'.$sold_product);
   $product_sold = $sold_product - $new_product;
   drupal_set_message('product sold :'.$product_sold);
   $new_node->field_product_stock['und'][0]['value'] = $product_sold;
   node_save($new_node);
 }
}

Now I want to do the same thing as done in uc_stock module of Ubercart.

1
  • Anyone? can suggest something??
    – aashi
    Jan 25, 2013 at 5:34

1 Answer 1

1

I'm not sure if I understood your question correctly, but if you want to change the stock for a product you can simply call the uc_stock_set() function (where the product is in the $node variable, and the new stock is in the $stock variable)

$stock = 5;
uc_stock_set($node->model, $stock); // sets stock to 5

Or use uc_stock_adjust() to increase or decrease the stock by a certain amount

uc_stock_adjust($node->model, 1); // adds 1 to current stock, stock is now 6

I am assuming that you have

  • Installed ubercart
  • Activated the uc_stock module
  • Created a product, and set its SKU (model) to a certain value, say MODEL_1
  • Gone to the 'stock' tab on the edit node page of this product and activated the stock for MODEL_1

At this point we have a product, represented by a node. This product has one (or many) associated SKUs. Each SKU has an associated stock stored in the database by uc_stock.

Only once all of this is in place can we can go ahead and use the uc_stock_set and uc_stock_adjust functions.

19
  • thanks penten for replying to my question. yes iam using uc_stock_adjust() like this:uc_stock_adjust($sku, $qty, $check_active = TRUE); ut it gives error of undefined variable
    – aashi
    Jan 25, 2013 at 6:10
  • and one more thing, what do you mean by $node->mode in uc_stock_adjust($node->mode, 1);?
    – aashi
    Jan 25, 2013 at 6:16
  • You have to pass the SKU of the product along with a variable containing the stock for that model. (I'm afraid I misspelt model as mode in the second example, updated)
    – penten
    Jan 25, 2013 at 6:23
  • can you help me with the code to pass the SKU of the product along with a variable containing the stock for the model. Actually im new to drupal.
    – aashi
    Jan 25, 2013 at 6:25
  • I have updated my answer. If you have a product node, the SKU (model) will be $node->model. The stock you want to set for this product will depend on what you're trying to do.
    – penten
    Jan 25, 2013 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.