0

This is my code:

function my_module_node_presave($node){
  if ($node->type == 'add_stock') {
    $stock_value = $node->field_stock_value['und'][0]['value'];
    drupal_set_message('stock_value :'.$stock_value);

    $sku = $node->field_sku['und'][0]['value'];
    drupal_set_message('sku :'. $sku);

    $key = $node->field_reference['und'][0]['nid'];
    drupal_set_message('key :'. $key);

    $key1 = node_load($key);
    //$pnode =  $key1->field_product_stock['und'][0]['value'];
    //drupal_set_message('key1 :'.$key1);
    //if (module_exists('uc_stock') && function_exists('uc_stock_adjust')) {

      uc_stock_set($node->$sku, $stock_value);
      uc_stock_adjust($node->$sku, $stock_value);

    //}
  }
}

The error I get is the following one:

Notice: Undefined property: stdClass::$tshirt-11 in my_module_node_presave() (line 40 of
D:\xampp\htdocs\photo\photogallary\sites\all\modules\my_module\my_module.module).

Notice: Undefined property: stdClass::$tshirt-11 in my_module_node_presave() (line 41 of D:\xampp\htdocs\photo\photogallary\sites\all\modules\my_module\my_module.module).

  • and same error if i use $key in place of $sku in last lines of code – aashi Jan 25 '13 at 10:34
  • it should be $node->sku or $node->key without the $ – Mohammed Shameem Jan 25 '13 at 10:35
  • after removing $ sign the error is Notice: Undefined property: stdClass::$sku in my_module_node_presave() (line 40 of D:\xampp\htdocs\photo\photogallary\sites\all\modules\my_module\my_module.module). Notice: Undefined property: stdClass::$sku in my_module_node_presave() (line 41 of D:\xampp\htdocs\photo\photogallary\sites\all\modules\my_module\my_module.module). – aashi Jan 25 '13 at 10:42
  • that means there is no variable called sku with in the node object. you could do something like drupal_set_message"<pre> . print_r($node, true) . "</pre>"); – Mohammed Shameem Jan 25 '13 at 10:49
3

When you write $node->$sku, PHP takes the value of $sku and uses it as name of a property.
In your case, $sku contains 'tshirt-11', and $node->$sku is equivalent to $node->tshirt-11. Since $node->tshirt-11 is not defined, you get the error you see.

The code you should write is the following one.

  uc_stock_set($sku, $stock_value);
  uc_stock_adjust($sku, $stock_value);

As side note, the value of a field is accessed using field_get_items(). The code you use to set $sku should be rewritten as the following.

$items = field_get_items('node', $node, 'field_sku');
$sku = $items[0];
  • ok the error is removed. but what im trying to do is not working. I want to increment the stock value of my product node.But that is not changing, it remains the same – aashi Jan 25 '13 at 10:48
  • As im using uc_stock_adjust($sku, $stock_value); to increment my product type node stock value which is referenced to the node on which im currently coding with my_module_node_presave($node) – aashi Jan 25 '13 at 10:50
  • That is another question. :) The question asks how to fix the error message, which is what this answer says. – kiamlaluno Jan 25 '13 at 10:56
  • if i do this:$items = field_get_items('node', $node, 'field_sku'); drupal_set_message('item :'. $item); $sku = $items[0]; drupal_set_message('sku :'. $sku); – aashi Jan 25 '13 at 11:02
  • it gives nothing in reply. and one more thing field_get_items('node', $node, 'field_sku'); is doing what? – aashi Jan 25 '13 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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