Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
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.
Solved! Go to the solution
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: ","}}]
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: ","}}]
Looks like you are on the right track with that code. Is something not working?
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.
Hey Paul I would love an answer this too as that code isnt working
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 %}]
Hi Paul,
Thanks for sending that through.
I tried the code and it's now outputting this error:
[{% 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
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: ","}}]
Add this as an example to the Flow help docs:
https://help.shopify.com/en/manual/shopify-flow/reference/variables#examples
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
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: ","}}]
Thank you, what a ball ache that was
As 2024 wraps up, the dropshipping landscape is already shifting towards 2025's trends....
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024