Managing replacement program via metafields (reading/writing product and order metafields)

Managing replacement program via metafields (reading/writing product and order metafields)

nick_reload
Visitor
3 0 0

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

 

flowz.jpgThis 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

 

1. Trigger is new order

2. Go through all the items in an order

3. 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

4. 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

Replies 3 (3)

paul_n
Shopify Staff
1361 152 319
For complicated aggregation scenarios, the Run code action is the best
solution.
Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
nick_reload
Visitor
3 0 0

oh interesting - I was trying to avoid that as it was javascript only. But if that has access to the right endpoints I can use it.

Could you point to any useful examples of Runcode javascript stuff for looping through the line items (and/or reading variant metafields)? and I can try and muddle my way through them?

nick_reload
Visitor
3 0 0

aha - getting somewhere - first node is RUN CODE:

export default function main(input) {

return {

variants: input.order.lineItems.map(item => item.variant.id.split('/').pop()),

}

}

 

And that gives me an array of variant id's in the order.

 

Next node is Get Product Variant Data

 

{% for variants_item in runCode.variants %}

{{variants_item}}

{% endfor %}

 

That gives me a list of variant data from which I can (for example) in a Log Output pull out the metafield I care about like this

 

{% for getProductVariantData_item in getProductVariantData %}

{{getProductVariantData_item.paddlecaremax.value}}

{% endfor %}

 

YAY...except NOT quite.

 

I now some kind of a Count or ForEach in order to increment things properly.

 

orrrr - I do it all in the run Code as you said 😉

 

Methinks Kalen is gonna figure it out for me...