Use computed field to calculate Field collection I have a field collection named values. It contains the fields:


Amount
Price
Total


The "Total" field calculates the Price*Amount.

Now I created a new field outside the field collection called "Subtotal".

How do I sum all the values of the "Total" field in a node to populate "Subtotal"?
I tried a lot of code which I found but none seem to work.

I tried this code but it gives as a warning: undefined index at line 5

$field_items = field_get_items($entity_type, $entity, 'field_values');
$total = 0;
foreach ($field_items as $item)
{
  $total += $item['field_total']['und'][0]['value'];
}
$entity_field[0]['value'] = $total;


Thank you in advance!
I've got it! After a lot of tries I found a solution:
This works for a computed field outside the collection.

$sum = 0;
$wrapper = entity_metadata_wrapper($entity_type, $entity);
foreach($wrapper-&gt;field_values as $collection) {
   $sum += $collection-&gt;field_total-&gt;value();
}
$entity_field[0]['value'] = $sum;