Looks like I need some @paul_n ninja help ![]()
Here’s what I’m trying to do. Think a replacement program where the customer gets a certain number of freebies and after that they have to pay.
The product has three variants. There’s an integer product variant metafield custom.paddlecareMAX which contains the number of maximum replacements for this variant purchase.
For the sake of argument the values here are
Variant ONE: 1
Variant PRO: 2
Variant MAX: 4
Sooo - when a new order is made - I need to sum through all the purchased variants and amend the integer STORE metafield custom.paddlecarebaseremaining with the summed up value
This way - when the customer decides to ask for a replacement unit the Return Service (be it loop or lateshipment or returnGO - haven’t yet decided) the customer can just type in an order number, they pull the order and when the customer says “Yeah I want to replace this.” - they can
(a) correctly look up how many Freebie items customer has left
(b) If >= 1 they say “Yay here’s your freebie replacement” and then deplete the counter by 1
(c) If == 0 they say “Yeah sorry you have to pay for it now”
(last but not least if the order is renewed the count can be reset)
But I’m a bit stuck with Shopify Flow with the counting logic.
I obviously can use ORDER CREATED as a trigger and the flow likely ends with UPDATE ORDER METAFIELD
But it’s the main bit - the counting and creating of a variable I can call in UPDATE ORDER METAFIELD that is the problem.
I’ve tried reading through a bunch of threads but I’m still a bit stuck (I did learn from Paul_n of course - that once metafields go into liquid, even if they are set as integers in shopify they are all strings - so maybe I cannot use them directly to calculate but I can bake in the logic.)
I found this code which seems like it can count correctly
{% assign sum_total = 0 %}
{% for li in order.lineItems %}
{% assign the_count = li.quantity | plus: sum_total %}
{% endfor %}
But I don’t want to count items of course - but values of a product variant metafield
What I’m trying to do is
-
Trigger is new order
-
Go through all the items in an order
-
For each item
EITHER (a) If I can do math with product variant metafield values
look up the value of custom.paddlecareMAX
Increment my counter
End up with total amount across all items bought once the for loop finishes
OR (b) If I cannot do math with product variant metafield values because they are strings in Liquid
look up the value of custom.paddlecareMAX
Based on string value custom.paddlecareMAX increment my counter with an integer (using IF/THEN hardcoding)
End up with total amount across all items bought once the for loop finishes
- At the end assign the total number from Step 3 to order level metafield - custom.paddlecarebaseremaining
I’m not sure how best to execute the count - do I use a For-Each loop to iterate over each item in the list?
And if yes - what do I do with each item? How do I pull the values of the variant metafield and store it in a variable I can later refer to from Update Order Metafield.
I look at using Count and looked at using Get Product Variant data - but in each case I get stuck.
It’s tantalisingly powerful but frustrating because I have no issues writing the pseudo-code but a ton of issues making it actually work.
Would love any guidance at all
