0

I had made two content types content type 1 - 'product' with field name 'product_stock' and content type 2 - 'Add stock' with field name 'stock_value'. It also contains a node reference to product content type. I want to add the value of product stock and stock value during node_presave.

For Example: product stock = 10 and stock value = 14, then both should be added to give 24.

5
  • And where do you want this computed value to be inserted / saved ? And in what order are your contents published ? Commented Jan 21, 2013 at 13:03
  • Do you want the 'product_stock' field to be updated with the addition of 'stock_value' field?
    – ermannob
    Commented Jan 21, 2013 at 14:40
  • to ermannob- yes, you got it right.Is der any way for this?
    – aashi
    Commented Jan 22, 2013 at 4:41
  • to Gregory- I want the 'product_stock' field to be updated with the addition of 'stock_value' field
    – aashi
    Commented Jan 22, 2013 at 4:49
  • Is there any body who can help me out with this?
    – aashi
    Commented Jan 22, 2013 at 6:02

1 Answer 1

-1
function my_module_node_presave($node){  // implementation of hook_node_presave
  if($node->type == 'add_stock') {
    $product = $node->field_stock_value['und'][0]['value']; //get data value of field
    drupal_set_message('product :'.$product); // print data value for checking
    $nid = $node->field_reference['und'][0]['nid']; //get nid of reference field
    drupal_set_message('nid :'.$nid); // print nid
    $p_node = node_load($nid); // implemenet node_load and get in $p_node
    $stock = $p_node->field_product_stock['und'][0]['value']; //get data value of field
    drupal_set_message('stock :'.$stock); // print value

    $stock = $product + $stock; // addition of both value
    drupal_set_message('Stock :'.$stock); 
    $p_node->field_product_stock['und'][0]['value'] = $stock; // updating the product field with updated value
    node_save($p_node); // implement node_save
  }
}

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.