How can I add 5% of a new order value to a customer's existing loyalty points using Flow?

Hello!

I’m experimenting with Shopify Flow to generate point accumulation for customers after each purchase. I’ve made a customer metafield, customer.metafield.loyalty.points set to JSON, that would store accumulated points value. Now I’m trying to make a Flow that would fetch the existing value of the metafield and add 5% of the new order value to it.

See the image attached - what code should I put in the value field in order to achieve this? Or am I getting the whole idea wrong and this should be done elsewhere rather than Flow?

1 Like

Hi A_oxolins,

Flow isn’t able to access metafields through the dot-delimited way that the Online Store editor can.

Liquid like this to replace the first line of your Value field may help:

{% assign accumulation = 0 %}
{% for metafields_item in order.customer.metafields %}
  {% if metafields_item.namespace == "loyalty" and metafields_item.key == "points" %}
    {% assign accumulation =  metafields_item.value %}
  {% endif %}
{% endfor %}

Hope that helps!

Thank you, Dave! That worked like a charm!

1 Like

@DaveMcV could you please help me? I have a similar issue to this, the code is outputting blank lines to Log output

The metafield looks like, eg, By (author) Marcus Rashford , Illustrated by Marta Kissi

{% assign author = '' %}
{% for metafields_item in product.metafields %}
  {% if metafields_item.namespace == "product" and metafields_item.key == "contributor" %}
    {% assign author =  metafields_item.value | split: "," | first | remove: "By (author) "%}
  {% endif %}
{% endfor %}
{{ author }}

@cybko That appears correct to me. Have you tried just assigning the metafield value without any filters to see what happens? It’s possible the value isn’t found or isn’t what you expect. If you are getting what you expect, then you could try applying the liquid filters one by one until you find the one causing the issue.

I have, yeah, it just outputs blankly even when the metafield isn’t blank (using the log output action). It’s definitely a single line text as well.

I actually just tried outputting ALL metafields, and it exists there.

It’s hard to know without some more details. Could you post screenshots of the workflow run log, the metafield definition, and the metafield value on the product from that run?

Hi Jeff, I’d made a mistake, I have it all sorted now. Thank you for responding so quickly and thoroughly.