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
- If you are using the E-commerce module the price value is available as
- 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.
Comments
Thanks for this
Thanks for this, it really saved my skin!
A little question
How can I add values of a multiple field ? I have an small idea but cannot find the small detail which makes everything ;)
Calculate the sum of fields from ALL Nodes of specific type
I am trying to configure a computed field to reflect the sum of all the values of field_a for a specific content type, and filter it by the user id of the user. I would like to get the UID from the url so that admin may view the same data as user without having to log in as user.
I am envisioning something like $count = db_result(db_query("SELECT SUM(field_a) from {content_type_x} WHERE USER ID = ID FROM URL"));
Thanks in advance for any help or suggestions.
same problem
i m also facing same problem i want to sum of fields of different types of nodes
add two values stored in 2 fields
Hello. I'm new in drupal computed field. I'm trying to add 2 values stored in 2 separate fields. Can anyone give me a hint??
drupal computed field calculating average
hi can anyone please post the code for calculating average of two numerical fields i am using computed field.thanks