How do I get the sum of integer values ​​displayed in the loop?

Comments

AndrzejG’s picture

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.

aa2007’s picture

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.

AndrzejG’s picture

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

aa2007’s picture

-- set a value: [node:field-weightcounter-ref:field-wcounter-weightcount] = added_weight

During the seting entity missing any variables (such as added_weight)

aa2007’s picture

Assigned: aa2007 » Unassigned
mitchell’s picture

Title: How do I get the sum of integer values ​​displayed in the loop? » How to: sum of integer values in a loop
Category: feature » support

Please document this when you find a solution.

Anonymous’s picture

Category: support » feature
Priority: Normal » Major

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!

Anonymous’s picture

Component: Rules Engine » Rules Core

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).

mradcliffe’s picture

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?

maciej.zgadzaj’s picture

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:

  • Add a variable: (for example) "total"
  • Loop
    • Calculate a value, input 1: [total], input 2: [current-line-item:commerce-unit-price:amount], return: "result"
    • Set a data value, data: [total], value: [result]
  • outsite of the loop I still have [total] value available
nvidian’s picture

Thanks a million, maciej.zgadzaj !!! You save my life !!!

mitchell’s picture

Version: 7.x-2.0-rc2 » 7.x-2.x-dev
Component: Rules Core » Documentation
Issue tags: +data transforms

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.

mrvinch’s picture

Re #10: Posted by maciej.zgadzaj on September 17, 2012 at 1:31pm

Hi,

Have you by chance used this process to perform the exact same calculation on a list of [commerce-price:amount] provided by the Views Rules action "Collect view result rows"?

The reason I ask is that the collected result rows always appear to be empty, even though the View itself provides the correct data. I've logged an issue over at View Rules but considering the similarity of your calculation I thought it would be worth asking here.

Cheers.

TR’s picture

Issue summary: View changes
Status: Active » Fixed

There are several answers here. #10 is the most straightforward and seems like the best answer.

Regardless, this question has been answered.

The other part of this issue is documentation, but as no-one has seen fit to document any of this and no-one has even commented on this issue in 7 years I have to question if anyone has even seen this ... Documentation may be added by anyone at anytime, and I don't see a need to keep this issue open any longer just in case someone finds this and decides to help out. There are too many missing parts to the documentation for the issue queue to be an effective way of tracking them, and prioritizing of documentation is better done in sprints or by whoever decides to write the documentation.

TR’s picture

Status: Fixed » Closed (fixed)
Gastonia’s picture

Given this is for documentation purposes, could someone please provide a comprehensive example of #10? I have been searching for days for a simple example of how to use the "Calculate a Value" selector that comes with Rules, yet they seem to be nonexistent on d.o., youbube, s.e, etc.. In fact, #10 is the closest I've seen to actual documentation about this functionality, but, being new to Rules, outside of the context of a full example, I have no idea where to put that. If I'm missing any existing documentation (that does not include talking about the Math module) please share that. I'm happy to add to any documentation once I figure it out. Thanks!