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.
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
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!
1 Like
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: " "â
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.
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)
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?