How to call function by reference? 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-&gt;type == 'add_stock') {
    //drupal_set_message('stock_save :'.print_r($node,true));
    //drupal_set_message($node-&gt;field_reference['und'][0]['nid']);
    //$nid=16;
    //$p_node = node_load($nid);
    //drupal_set_message($p_node-&gt;field_product_stock['und'][0]['value']);
    $product = $node-&gt;field_stock_value['und'][0]['value'];
    drupal_set_message('product :'.$product);
    $nid = $node-&gt;field_reference['und'][0]['nid'];
    drupal_set_message('nid :'.$nid);
    $p_node = node_load($nid);
    $stock = $p_node-&gt;field_product_stock['und'][0]['value'];
    drupal_set_message('stock :'.$stock);

    $stock = $product + $stock;
    drupal_set_message('Stock :'.$stock);
    $p_node-&gt;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-&gt;type == 'sold_stock') {
   $new_product = $node-&gt;field_sold_stock['und'][0]['value'];
   drupal_set_message('new product :'.$new_product);
  $new_nid = $node-&gt;field_stock_reference['und'][0]['nid'];
   drupal_set_message('new nid :'.$new_nid);
   $new_node = node_load($new_nid);
  $sold_product = $new_node-&gt;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-&gt;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.
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-&gt;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-&gt;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.