Last updated 25 September 2013. Created on 25 September 2013.
Edited by Sree. Log in to edit this page.

Here is an example code where the requirement is to calculate 'Final Price' based on the number of items the user is willing to purchase.

Base Price: Price per unit.
No Of Items: Number of copies of the commodity.
Final Price: Total price of that commodity.

1. Create the corresponding fields in the CCK.
2. Create a module folder under your site's custom modules.
3. Add the 'Computed Field' dependency in the .info file of the module.
4. Create the .module file & provide the following code

* Computed field : field_final_prize
* The variables available to your code include: 
* &$entity_field, $entity_type, $entity, $field, $instance, $langcode, and $items. 
* To set the value of the field, set $entity_field[0]['value']. 
* For multi-value computed fields continue with $entity_field[1]['value']. 
* Here's a simple example which sets the computed field's value to the value of the sum of the number
* $entity_field[0]['value'] = 
*     array_pop(array_pop(field_get_items($entity_type, $entity, 'field_a'))) + 
*     array_pop(array_pop(field_get_items($entity_type, $entity, 'field_b'))); 
* The first pop fetches the last (or only) item from the field while the second pop fetches its 
* ['value'] contents (assuming it's the only key that's set).
function computed_field_field_final_prize_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items){
  $entity_field[0]['value'] = 
    $entity->field_base_prize['und'][0]['value'] *

If you use Commerce Price field for base price then


computed_field_FIELD_NAME_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items)
computed_field_FIELD_NAME_display($field, $entity_field_item, $entity_lang, $langcode)
These two are the functions provided in the guide & you can write your custom code in your custom module in these functions for computing or custom display requirements.

Computed field only generate values on node save. So, be sure to test your custom code accordingly.

In some cases writing your code in the text area provided by the module doesnt work. So, this is a very small exmple showing you how you can write a custom module for the computed field requirements.

Looking for support? Visit the forums, or join #drupal-support in IRC.