Sum values in array
I'm having trouble figuring out how to iterate over an array and sum the values. Here is where I am so far. Is there another way to do this?
{{#each discount_applications}}{{sum this.value}}{{/each}}
Result: {"discount_amount": "36.113.99120"}
Context json:
{...
"discount_applications": [
{
"target_type": "line_item",
"type": "manual",
"value": "36.11",
"value_type": "fixed_amount",
"allocation_method": "each",
"target_selection": "explicit",
"title": "Capturing Fixed Discount",
"description": "Capturing Fixed Discount"
},
{
"target_type": "shipping_line",
"type": "script",
"value": "3.99",
"value_type": "fixed_amount",
"allocation_method": "across",
"target_selection": "explicit",
"title": "Free Standard Shipping",
"description": "Free Standard Shipping"
},
{
"target_type": "line_item",
"type": "discount_code",
"value": "120.0",
"value_type": "fixed_amount",
"allocation_method": "across",
"target_selection": "all",
"code": "CXGLM-3X-jrshifflett270"
}
],
...}
Comments
It would be nice if that worked but there's no way to do it using #each.
A couple of work arounds...
1. Use transformation 2.0 to iterate through array and populate a string array with your values. You can then use Sum on that array.
2. Use a script. You can probably use the AI if you're not a coder to write that script for you.
David Gollom the transformation to string array is a very interesting way. Thanks for sharing
Happy to help!
Please sign in to leave a comment.