What's your biggest current challenge? Have your say in Community Polls along the right column.

Trying to increment a customer metafield for each item in an order.

Solved

Trying to increment a customer metafield for each item in an order.

MVS-ONE
Shopify Partner
17 0 3

Currently I'm trying to create a flow where it loops through each item in an order and increments an integer field on the customer's profile:

 

Screenshot 2024-11-13 at 12.27.38 pm.png

When the Flow runs, it can see it successfully looping through each item in the order:

 

Screenshot 2024-11-13 at 1.07.04 pm.png

 

But the field isn't being incremented by 1 for each item as expected. If an order has 3 items in it, it will still just increment the field by 1 for the whole order.

 

If I make a new order, the field will be incremented by 1 each time, so I can see it's at least reading the existing value correctly.

 

Accepted Solution (1)
tim
Shopify Partner
3911 394 1435

This is an accepted solution.

Ah, that changes a lot.

In this case I'd rather jump to "update metafield" straight from the trigger and loop over line items and count in the liquid code in metafield value.

 

If not -- adding logging and little sleep (using Wait action) inside your loop may help if the problem is with multiple consecutive metafield updates failing.

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com

View solution in original post

Replies 4 (4)

tim
Shopify Partner
3911 394 1435

Why don't you calculate first and then update once?

Multiple consecutive MF updates are never good...

Screenshot 2024-11-13 at 5.20.43 PM.png

(my key does not match yours)

 

Update: the below seems to not be an issue:

Also your existing metafield reference seems odd, I'd use {{sum | plus: order.customer.metafields.custom.total_Items.value }} , but looks like they've added a new way or referencing metafields --  https://changelog.shopify.com/posts/shopify-flow-access-typed-metafields 

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
MVS-ONE
Shopify Partner
17 0 3

Sum is a good lead (I didn't know it existed, I just started using Flow), but I also need to be able to count only products that are in a certain collection in the order. 

tim
Shopify Partner
3911 394 1435

This is an accepted solution.

Ah, that changes a lot.

In this case I'd rather jump to "update metafield" straight from the trigger and loop over line items and count in the liquid code in metafield value.

 

If not -- adding logging and little sleep (using Wait action) inside your loop may help if the problem is with multiple consecutive metafield updates failing.

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
MVS-ONE
Shopify Partner
17 0 3

Ditching the loop option in Flow and just doing it in liquid was the answer:

 

{%- assign counter = order.customer.stateMachinePurchases.value -%}
{%- for a in order.lineItems -%}
  {%- for b in a.product.collections -%}
    {%- if b.title == "State Machine" -%}
      {%- assign counter = counter | plus: 1 -%}
    {%- endif -%}
  {%- endfor -%}
{%- endfor -%}
{{- counter -}}

 


It's all good now. Thanks Tim!