'Total array of line item values in liquid
I have an array of line items that I would like to output the total of all of the price attribute.
JSON:
"lineItems":
[
{
"code": "000001",
"description": "Product 1",
"quantity": 1,
"price": 30
},
{
"code": "000002",
"description": "Product 2",
"quantity": 2,
"price": 10
}
]
Expected Result from above: 50
Workings: (1 x 30) + (2 x 20)
Using Liquid, how do I output the desired result?
I tried something like the following, as I couldn't work it out.
{% assign totalX = lineItem.price %}
{{ totalX | join: plus }}
Solution 1:[1]
{{lineItems | map: "price" | sum}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tom Bunn |
