Posted by aa2007 on December 26, 2011 at 7:32pm
8 followers
Jump to:
| Project: | Rules |
| Version: | 7.x-2.x-dev |
| Component: | Documentation |
| Category: | feature request |
| Priority: | major |
| Assigned: | Unassigned |
| Status: | active |
| Issue tags: | data transforms |
Issue Summary
How do I get the sum of integer values displayed in the loop?
Comments
#1
Should be more specific.
1. If they are displayed in the succeeding loops, I see no way, as there is no way to create a variable global with respect to the loop. This is big disadvantage of the Rules.
2. If they are displayed in one loop, first create a variable and then add integers to this variable.
#2
describe my situation:
I need to determine the weight of all goods ordered and round up to 1 kilogram.
On counting the weight of each item there are no problems, I made it through this screencast http://vimeo.com/33838479
But as the total weight of round I can not understand.
#3
So it seems to me to be a case #1.
My comment re this case was not complete, because there IS a way to bypass the limitation (lack of "global" variable), albeit complicated a little.
My idea (not sure, of course) is to keep a sum in a field of separate node type:
Try to create another content type, say Weight Counter (wcounter) with a field summing up the weights (node:field-wcounter-weightcount). Create also a node (entity?) reference field in Product (node:field-weightcouter-ref_, and make it default to Weight Counter. Rules for each loop should call to this field, add a Product weight and save Weight Counter immediately (!). After looping is finished, make use of the sum, and don't forget to reset weightcount field to zero, in order to make it prepared for another transaction.
Something like that:
- Event triggering Rules action
- Condition: node-type [node:field-weightcounter-ref] has field [node:field-wcounter-weightcount]
- Loop:
-- calculate a value: [node:field-weightcounter-ref:field-wcounter-weightcount] + [node:field-product-weight] = added_weight
-- set a value: [node:field-weightcounter-ref:field-wcounter-weightcount] = added_weight
-- save entity: [node:field-weightcounter-ref] Immediately
after looping:
- make use of the sum, [node:field-weightcounter-ref:field-wcounter-weightcount] (probably calculate shipping cost or what You wish)
- set a value: [node:field-weightcounter-ref:field-wcounter-weightcount] = 0
- save entity [node:field-weightcounter-ref]
HTH
#4
During the seting entity missing any variables (such as added_weight)
#5
#6
Please document this when you find a solution.
#7
I think, when looping through a list of entities, it is currently not possible to calculate the total of a certain field.
Rules provides two actions that should help:
A) set a value
B) calculate a value
A is meant to set a value and (optional) add an offset. The offset is a constant value, so can't be used for adding up fields. A solution for this issue would be to enable "set a value" for using nonconstant offsets (ie. fields, variables).
B is able to sum two nonconstant values (ie. fields, variables), but the target has to be a new variable (existing variables aren't allowed). In a loop this can't work. The solution regarding B would be to assign the result (like A does).
Please correct me if I missed something!
Even for a limitted number of values - lets say 3 values (v1 + v2 + v3) - you'll have to add v1 + v2 and assign them to an intermediate result variable (r1). Then r1 + v3 and assign to r2. After wards r2 may be assigned to a field or - in case of a component - to a provided variable.
This is far from being user friendly and is IMHO no documentation issue, but a conceptual problem. That said, I hope I'm wrong!
#8
A solution is discussed at #1600272: Calculating with lists: sum, mean, count, min, max.
There you find a custom module (beware, it's unsupported prototype code).
#9
This also effects text values.
1. Add a variable outside of loop.
2. Inside loop, set data value, use the previously defined variable, and concatenate the new text to the defined variable token.
3. Can't use variable defined outside of loop.
Is #1234000: loops do not save changes of variables going out of scope also related to this issue? Or is this a duplicate of that issue?
#10
For me it works pretty well without any additional modules, just Rules 2.2. Using Drupal Commerce, I needed to sum prices of all product in my shopping cart:
#11
Thanks a million, maciej.zgadzaj !!! You save my life !!!
#12
Please consider adding this as documentation to LOOPs and providing an export so 'total' / 'sum' can be reused more easily, even provided as a default component by a module.