Calculating a value in a loop

I am building a very simple points program using Shopify Flow.

I calculate the amount of points I get in a loop (because I have a condition that I need to check for each item) and add the result of my points calculation to a metafield. I want to take into account the previous value of the metafield. I thought of storing the intermediary result and then passing that back to the loop, but I do not know how.

Here is the value in the update metafield block:

{{lineItemsForeachitem.variant.price | times: lineItemsForeachitem.quantity | plus: order.customer.kerstpunten1.value | default: 0}}

Here is the flow:

This code is what I’d start with.
Do not need to store value elsewhere if it’s written to the MF.

But it would be better this way:

{%- assign previous = order.customer.kerstpunten1.value | default: 0 -%}
{{-  lineItemsForeachitem.variant.price | times: lineItemsForeachitem.quantity | plus: previous -}}

This can be problematic though if there are multiple items in order and there would be a bunch of MF updates fired in succession …

So, instead of looping over line items with Flow action, you can do one metafield update, but do looping in liquid code. Kinda like this (I just used arbitrary condition in if – use yours…) (not fully tested though! : )

{%- liquid 
  assign mf_value = order.customer.kerstpunten1.value | default: 0 
  for item in order.lineItems 
    if item.product.tags contains "tag"
      assign mf_value = mf_value | plus: item.originalTotalSet.shopMoney.amount
    endif
  endfor 
-%}
{{ mf_value }}

originalTotalSet definition

In shop and presentment currencies, the total price of the line item when the order was created. This value doesn’t include discounts.

Yeah, two options right now: do it in liquid, or do it via Run code.