Processing Field collection items with Rules is fun, really! Have a look at this Rule (in Rules export format):
{ "rules_calculate_sum_of_prices_in_all_field_collection_items" : {
"LABEL" : "Calculate sum of prices in all field collection items",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "node_view--article" : { "bundle" : "article" } },
"IF" : [
{ "entity_has_field" : { "entity" : [ "node" ], "field" : "field_article_details" } }
],
"DO" : [
{ "drupal_message" : { "message" : "\u003Cstrong\u003EDrupal calculator\u003C\/strong\u003E started ..." } },
{ "variable_add" : {
"USING" : { "type" : "decimal", "value" : "0" },
"PROVIDE" : { "variable_added" : { "total_price" : "Price total" } }
}
},
{ "LOOP" : {
"USING" : { "list" : [ "node:field-article-details" ] },
"ITEM" : { "article_details_item" : "Article details item" },
"DO" : [
{ "data_calc" : {
"USING" : {
"input_1" : [ "total-price" ],
"op" : "+",
"input_2" : [ "article-details-item:field-price" ]
},
"PROVIDE" : { "result" : { "calculation_result" : "Calculation result" } }
}
},
{ "data_set" : { "data" : [ "total-price" ], "value" : [ "calculation-result" ] } },
{ "drupal_message" : { "message" : "After adding a price of \u003Cstrong\u003E[article-details-item:field-price]\u003C\/strong\u003E for field collection item with id \u003Cstrong\u003E[article-details-item:item-id]\u003C\/strong\u003E, subtotal is \u003Cstrong\u003E[calculation-result:value]\u003C\/strong\u003E." } }
]
}
},
{ "drupal_message" : { "message" : "The \u003Cstrong\u003ETotal price\u003C\/strong\u003E for all prices included as field collection items is \u003Cstrong\u003E[total-price:value]\u003C\/strong\u003E." } },
{ "drupal_message" : { "message" : "\u003Cstrong\u003EDrupal calculator\u003C\/strong\u003E ended ..." } }
]
}
}
Some more details about this rule are below ...
Content is viewed (of type Article), adapt the machine name of the content type article
to whatever fits (or use any other Rules Event that fits).
Entity has field, whereas the entity is "node", and the machine name of my field collection field is field_article_details
(adapt this machine name to whatever fits, but make sure you use the field collection field itself).
Wake up, here is where the magic (fun?) is going to happen ... These are the Rules Actions involved:
Drupal calculator started ...
Add a variable, whereas it is a variable named total_price
, decimal (2 digits), initial value 0.
Add a loop, to iterate over each item of my field collection field (with machine name field_article_details
), and perform these Rules Actions for each iteration:
Calculate a value, which calculates the sum of total_price
(defined in Rules Action 2 above) and article-details-item:field-price
(this is the machine name of the field in the field collection that contains the prices, decimal with 2 digits), and stores the result (sum) in variable calculation_result
.
Set a data value, which simply copies the value stored in variable calculation_result
in my total_price
(defined in Rules Action 2 above). Remark: not sure (not tested), but maybe this calculation_result
variable can be replaced straight by total_price
(in the previous action), so that you would not need this action.
Show a message on the site, with a message like so:
After adding a price of 3.40 for field collection item with id 3, subtotal is 15.00.
The Total price for all prices included as field collection items is 26.23.
Drupal calculator ended ...
Obviously, this rule is rather a prototype. After you're convinced it works as it should, just remove all Rules Actions with Show a message on the site. So that only item 2 and 3 (without its last sub-bullet) is left as Rules Actions.
Here is a sample of my test results, i.e. the Drupal messages that are shown:
Drupal calculator started ...
After adding a price of 2.45 for field collection item with id 1, subtotal is 2.45.
After adding a price of 9.15 for field collection item with id 2, subtotal is 11.60.
After adding a price of 3.40 for field collection item with id 3, subtotal is 15.00.
After adding a price of 1.23 for field collection item with id 4, subtotal is 16.23.
The Total price for all prices included as field collection items is 26.23.
Drupal calculator ended ...
If you're not familiar with field collections, try to first digest the answer to "this question".