Re: Tags to metafield - single line list

Solved

How can I convert product tags into metafield single line lists?

dh_harrypapas
Tourist
10 1 1

Currently, we tag our products with various attributes for products, such as Category and Colour. These tags are prefixed with "Category: " or "Colour: " as an example.

 

We've got a flow setup where we want the flow to read the tags on the product extract ones that start with a prefix and add those in a list of single line strings.

 

Does anyone know how this can be achieved. These metafields will populate our filters on collection pages.

 

Screenshot 2023-04-20 at 12.14.51 pm.png

Accepted Solutions (2)
paul_n
Shopify Staff
1160 143 275

This is an accepted solution.

I put the comma thing in the wrong spot. But the problem is that putting it inside the first if statement means you can't use "forloop.last".  I used this code awhile back to solve the same problem. It assigns a comma separated list to a new variable and then strips the last comma and adds the square brackets. The "strip" parts removes any whitespace. The hyphens remove whitespace around the tags so it's easier to read the code.

 

{% capture mf_value %}
{%- for tags_item in product.tags -%}
{%- if tags_item contains "Colour:" -%}
"{{- tags_item | remove_first: "Colour:" | strip -}}",
{%- endif -%}
{%- endfor -%}
{% endcapture -%}
[{{mf_value | remove_last: ","}}]

 

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

dh_harrypapas
Tourist
10 1 1

This is an accepted solution.

All good. Worked it out Just had to add an unless

 

{% capture mf_value %}
{%- for tags_item in product.tags -%}
{%- if tags_item contains "Category: " -%}
{% unless tags_item contains "Sub-Category: " %}
"{{- tags_item | remove_first: "Category: " | strip -}}",
{%- endunless -%}
{%- endif -%}
{%- endfor -%}
{% endcapture -%}
[{{mf_value | remove_last: ","}}]

 

View solution in original post

Replies 10 (10)

paul_n
Shopify Staff
1160 143 275

Looks like you are on the right track with that code. Is something not working?

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.
dh_harrypapas
Tourist
10 1 1

It only adds one value in the single line text metafield type, where as I want to have it as a list of single line strings and loop through all the tags for multiple colour tags.

BenBritton
Tourist
4 0 2

Hey Paul I would love an answer this too as that code isnt working

paul_n
Shopify Staff
1160 143 275

That list takes the form of 

["item1", "item2"]

So you need to include the brackets, commas, and quotes. 

[{% for tags_item in product.tags %}{% if tags_item contains "Colour" %}"{{ tags_item | remove: "Colour" }}"{% endif %}{% if forloop.last %}{% else %},{% 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.
dh_harrypapas
Tourist
10 1 1

Hi Paul,

 

Thanks for sending that through.

 

I tried the code and it's now outputting this error:

 

Screenshot 2023-05-03 at 10.58.33 am.png

[{% for tags_item in product.tags %}{% if tags_item contains "Colour: " %}"{{ tags_item | remove: "Colour: " }}"{% endif %}{% if forloop.last %}{% else %},{% endif %}{% endfor %}]

The meta field type I'm trying to populate is "List of single line strings".

 

Thanks,

Harry

paul_n
Shopify Staff
1160 143 275

This is an accepted solution.

I put the comma thing in the wrong spot. But the problem is that putting it inside the first if statement means you can't use "forloop.last".  I used this code awhile back to solve the same problem. It assigns a comma separated list to a new variable and then strips the last comma and adds the square brackets. The "strip" parts removes any whitespace. The hyphens remove whitespace around the tags so it's easier to read the code.

 

{% capture mf_value %}
{%- for tags_item in product.tags -%}
{%- if tags_item contains "Colour:" -%}
"{{- tags_item | remove_first: "Colour:" | strip -}}",
{%- endif -%}
{%- endfor -%}
{% endcapture -%}
[{{mf_value | remove_last: ","}}]

 

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.
paul_n
Shopify Staff
1160 143 275

Add this as an example to the Flow help docs:

https://help.shopify.com/en/manual/shopify-flow/reference/variables#examples

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.
dh_harrypapas
Tourist
10 1 1

Thanks Paul. This works great.

 

Only thing is we have tags that begin with Category: and Sub-Category: and it appears they're chasing. Is there a way this can be altered so we don't have to adjust all of our tags?

 

You'll see in the Category metafield, it's showing Sub-Romper

 

Screenshot 2023-05-04 at 11.04.51 am.png

dh_harrypapas
Tourist
10 1 1

This is an accepted solution.

All good. Worked it out Just had to add an unless

 

{% capture mf_value %}
{%- for tags_item in product.tags -%}
{%- if tags_item contains "Category: " -%}
{% unless tags_item contains "Sub-Category: " %}
"{{- tags_item | remove_first: "Category: " | strip -}}",
{%- endunless -%}
{%- endif -%}
{%- endfor -%}
{% endcapture -%}
[{{mf_value | remove_last: ","}}]

 

RococoDigital
Shopify Partner
13 1 11

Thank you, what a ball ache that was