Re: Return metafield value, then do addition

Solved

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

a_ozolins
Shopify Partner
4 0 2

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?

 

Screenshot 2023-03-24 110140.png

Accepted Solution (1)

DaveMcV
Shopify Staff
104 31 29

This is an accepted solution.

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!



 

DaveMcV | Flow Development 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.

View solution in original post

Replies 7 (7)

DaveMcV
Shopify Staff
104 31 29

This is an accepted solution.

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!



 

DaveMcV | Flow Development 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.
a_ozolins
Shopify Partner
4 0 2

Thank you, Dave! That worked like a charm!

cybko
Shopify Partner
44 0 8

@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 }}
Jeff-Armstrong
Shopify Staff
35 3 3

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

To learn more visit the Shopify Help Center or the Community Blog.

cybko
Shopify Partner
44 0 8

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.

Jeff-Armstrong
Shopify Staff
35 3 3

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?

To learn more visit the Shopify Help Center or the Community Blog.

cybko
Shopify Partner
44 0 8

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