Computed Fields: Adding numeric values stored in two separate fields (basic info).

Last modified: October 31, 2009 - 11:40

Note: the Computed Field Module can be used instead of this custom code (in Drupal 5.x and 6.x)

Imagine you have two existing number fields, called field_product_price and field_postage_price. You want to create a computed field field_total_cost which adds these two fields. Create a new computed field with the name 'Total Cost', and in your computed field's configuration set the following:

  • Computed Code:$node_field[0]['value'] = $node->field_product_price[0]['value'] + $node->field_postage_price[0]['value'];
    • If you are using the E-commerce module the price value is available as $node->price
    • If you are using the UBERCART module the public price value is available as $node->sell_price
  • Check 'Display this field'
  • Display Format:$display = '' . $node_field_item['value'];
  • Check 'Store using the database settings below'
  • Data Type: float
  • Data Length: 10,2
  • Default Value: 0.00
  • Check 'Not NULL'
  • Check 'Sortable'

While the example above uses a price and postage which both can be handled using ecommerce modules or Ubercart, there are other examples when you would want to compute two fields and the above instructions/example provide guidance.

Thanks for this

Parkes Design - September 11, 2009 - 07:02

Thanks for this, it really saved my skin!

A little question

gregoiresan - November 16, 2009 - 15:30

How can I add values of a multiple field ? I have an small idea but cannot find the small detail which makes everything ;)

Greg

 
 

Drupal is a registered trademark of Dries Buytaert.