Solved

Adding metafield value to tag with prefix

jake_mitchell
Shopify Partner
120 2 53

Hi, 

 

I recently asked about whether it is possible to add adding a tag to metafield after first removing prefix 

 

e.g. brand_adidas tag becomes adidas in metafield. 

 

But what about the opposite. 

 

Is it possible to take the value of a metafield and convert it into a tag with a prefix. 

 

Something like...

 

- Trigger = product added to store

- Condition = metafield namespace and key matches to metafield we want value of

- Action = add metafield value to tag with prefix (e.g. 'brand_')

 

e.g. 

 

I list an item and add Adidas to the metafield 'brand' 

Once the flow is complete the item should have a tag of 'brand_Adidas'.

Accepted Solutions (2)

paul_n
Shopify Staff
1056 136 250

This is an accepted solution.

Yes, you can do that in liquid. Assuming it's for an order, you would put this (remove any line break) in the tag field.

{% for mf in order.metafields %}

{% if mf.namespace == "custom" and mf.key == "key-name" %}

prefix_{{ mf.value}}

{% endif %}{% endfor %}

 

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.

View solution in original post

jake_mitchell
Shopify Partner
120 2 53

This is an accepted solution.

You're a genius. Thank you so much, Paul. That worked! 

 

You've really helped us out there, and in doing so hopefully helped us to make more money for our cause. 

 

Now, just need to decide which of your many solutions is the one to flag as the solution. 

 

For the benefit of anyone else reading this the correct liquid ended up being 

 

{% for mf in product.metafields %} {% if mf.namespace == "product" and mf.key == "letter_size" %} Size: {{ mf.value | replace: ",", ", Size: " | remove: "[" | remove: "]" | remove: '"' }} {% endif %}{% endfor %}

View solution in original post

Replies 8 (8)

paul_n
Shopify Staff
1056 136 250

This is an accepted solution.

Yes, you can do that in liquid. Assuming it's for an order, you would put this (remove any line break) in the tag field.

{% for mf in order.metafields %}

{% if mf.namespace == "custom" and mf.key == "key-name" %}

prefix_{{ mf.value}}

{% endif %}{% endfor %}

 

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

Hi Paul, 

Thanks for this response. I was actually looking for a flow that would work on the product being added to the store. 

It doesn't look like the 'add product tags' allows for liquid

jake_mitchell_0-1659348217066.png

 

paul_n
Shopify Staff
1056 136 250

You need to hit enter after adding that liquid

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

That's perfect. Thank you. 

It works for that. 

I've set it up to also work from a metafield that has a list of set values. example below: 

jake_mitchell_0-1659357951333.png

 

I've used the liquid below for this

 

{% for mf in product.metafields %} {% if mf.namespace == "my_fields" and mf.key == "category" %} {{ mf.value}} {% endif %}{% endfor %}

The flow works, but has a bit of an issue in that it creates tags that include [ or ] or " in the tag value. 

jake_mitchell_1-1659358044677.png

 

Do you know if there is a way of stripping this out? 

Thanks again for all your help. This is going to be really useful stuff for us. we're a charity with lots of people (including volunteers) listing items and we're trying our best to create a way of making the listing process as easy as possible and having as few possibilities for human error as possible. 


This is the full flow by the way: 

jake_mitchell_2-1659358210143.png

 

paul_n
Shopify Staff
1056 136 250

The list type is new to Flow so I'm not terribly familiar with it yet. Based on that screenshot, it looks like it's treating the values as a string and then splitting by the comma(s) because that's what the tag action does to allow multiple tags in one liquid statement. If you want each of those values as a tag, then you should try to remove the quotes and the brackets. Something like this:

 

{{ mf.value | remove: "[" | remove: "]" | remove: '"' }}

 Here's docs on this:

https://shopify.dev/api/liquid/filters#remove

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

Thanks again. 

I'm very nearly there. What you've suggested for stripping out the characters works great. 

The flow works great with one small exception. 

Where a metafield allows for multiple selections AND should be prefixed when added as a tag the flow will add the prefix to the first metafield value but not any subsequent ones. 

e.g. I'm adding a letter size for a cardigan. I used the metafield for this as below

 

jake_mitchell_0-1659367208524.png

 

The flow runs as expected and tags are added, but the prefix is added only to S

 

jake_mitchell_1-1659367269929.png

 

I have tried the liquid tag action as both the prefix version (A) from above and also as prepend: "Size: " (B)

 

A - {% for mf in product.metafields %} {% if mf.namespace == "product" and mf.key == "letter_size" %} Size: {{ mf.value | remove: "[" | remove: "]" | remove: '"' }} {% endif %}{% endfor %}

 

B - {% for mf in product.metafields %} {% if mf.namespace == "product" and mf.key == "letter_size" %} {{ mf.value | prepend: "Size: " | remove: "[" | remove: "]" | remove: '"' }} {% endif %}{% endfor %}

 

 

Both variations will give me nearly correct tags but with the "Size: " prefix only added for the first value selected. 

Sorry, I know this is a lot to ask, but do you have any idea why that is? 

 

 

 

paul_n
Shopify Staff
1056 136 250

What is the character in that size between S and M? It looks like it's being treated as comma. So when this runs outputs 

Size: S, M

The tag action then splits them by the comma so you get "Size: S" and "M".

 

Assuming you want "Size: S" and "Size: M" I might just replace the comma with ", Size: ". So like this:

{{ mf.value | replace: ",", ", Size: " }}

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

This is an accepted solution.

You're a genius. Thank you so much, Paul. That worked! 

 

You've really helped us out there, and in doing so hopefully helped us to make more money for our cause. 

 

Now, just need to decide which of your many solutions is the one to flag as the solution. 

 

For the benefit of anyone else reading this the correct liquid ended up being 

 

{% for mf in product.metafields %} {% if mf.namespace == "product" and mf.key == "letter_size" %} Size: {{ mf.value | replace: ",", ", Size: " | remove: "[" | remove: "]" | remove: '"' }} {% endif %}{% endfor %}