Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Add customer metafield from product metafields

Add customer metafield from product metafields

jake_mitchell
Shopify Partner
120 2 65

Hi, 

 

We have some metafields listed on our products. 

 

For some of those we have associated customer metafields that mirror them. 

 

For example, against the product we may have a department metafield that includes values like: 

 

  • menswear
  • womenswear
  • homeware

There is a mirror metafield for customers. 

 

So that we can have a record of the dept the customer has purchased from we would like to be able to use flow to move the product metafield value into the customer metafield. 

 

This is what I tried: 

 

  • Trigger: order created
  • Action: update customer metafield

The liquid I have used there is:

 

{% for mf in product.metafields %}

   {% if mf.namespace == "product" and mf.key == "department" %}

     {{ mf.value }}

   {% endif %}

{% endfor %}

 

Sadly I get a load of errors that say 'product' is invalid. 

 

jake_mitchell_0-1665159954883.png

 

Does anyone know if it is possible to move the MF value used on the product to the customer. Ideally I wouldn't have to do this with a messy workaround like taking it from tags because that would be a much, much messier flow to set up and upkeep. 

 

Thanks

Replies 4 (4)

DaveMcV
Shopify Staff
104 31 29

Hi Jake_mitchell,

 

Your loop is constructed without knowing what `product` you're operating on because this is an `Order Created` trigger. You'll need to loop through the `order.lineItems.products` to get all the products for the Order. Keep in mind, this may result in multiple products matching the metafield criteria.

 

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.
jake_mitchell
Shopify Partner
120 2 65

Thank you @DaveMcV 

I've gotten the flow working using: 

{% for mf in order.lineItems.product.metafields %}
{% if mf.namespace == "product" and mf.key == "department" %}
{{ mf.value }}
{% endif %}
{% endfor %}

But when it runs I get the error message of "Got error updating metafield: "Value can't be blank." For value: " "' 

jake_mitchell_0-1665399987417.png

 

Do you know why this might be? From the wording it sounds like we can't use the action to update a metafield where the value is currently blank. Is that correct? 

If so, that would be a blocker to this whole thing. 

 

jake_mitchell
Shopify Partner
120 2 65

Sorry to bug you @DaveMcV  but do you have any further thoughts on the above? 

The error message isn't that helpful (or maybe I just don't understand it)

jake_mitchell
Shopify Partner
120 2 65

Got it working with

{% for lineItems_item in order.lineItems %}
{% for metafields_item in lineItems_item.product.metafields %}
{% if metafields_item.namespace == "namespace" and metafields_item.key == "key" %}{{ metafields_item.value }}
{% endif %}
{% endfor %}
{% endfor %}

The thing I now want to check out is whether it is possible to set these up in such a way that for customers who come back multiple times it doesn't overwrite the metafields, but instead adds to them. 

 

@DaveMcV  is that possible?