Calculating a value in a loop

Topic summary

A user is building a points program in Shopify Flow and needs to calculate points by looping through order line items while accumulating values in a customer metafield. The challenge is maintaining and updating the running total across loop iterations.

Initial approach: Updating the metafield within each loop iteration, attempting to add the previous metafield value to each calculation.

Recommended solutions:

  • Option 1 (Liquid within metafield update): Use Liquid code to handle the entire loop in a single metafield update, avoiding multiple rapid-fire updates. This involves assigning the previous metafield value, looping through order.lineItems, applying conditions, and accumulating the total before writing once.

  • Option 2: Use Flow’s “Run code” action for more complex logic.

Key consideration: Multiple metafield updates firing in quick succession (one per line item) can be problematic, making the single-update approach preferable.

The discussion includes sample Liquid code demonstrating how to loop through line items, check conditions, and calculate the accumulated points value in one operation.

Summarized with AI on October 24. AI used: claude-sonnet-4-5-20250929.

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.

1 Like