Problem using Flow and order metafields to get total product costs of each order

I am trying to set up flow to calculate the total product cost of each order and add it to an order metafield. I have attached a screenshot of the flow below:

The problem is I seem to only receive the costing of the last item in the lineitem loop. I think the problem is that the metafield isn’t updating quickly enough for the flow, but have no idea on how to workaround this, as you cannot add a ‘wait’ to for loops??

Any help would be greatly appreciated!!

Thanks

1 Like

You can’t reuse information between iterations of a loop. Three options:

  1. Use an existing “total” field available on the order
  2. Use a Run code action to do the calculation (in JavaScript) - most likely this is what you need to do
  3. Use liquid in the update metafield action to do the calculation

There are examples of how to do totals on orders here: https://github.com/Shopify/flow-code-examples/tree/main/run-code-examples

For future reference, and for anybody who has the same issue. As Paul suggested, I solved the issue with option 2 by writing some javascript in ‘Run Code’ to do the calculations.

Hi Matty! would you be so kind as to share the javascript you used? I can’t seem to get the cost of a line item.
Many thanks.