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

How to use Shopify Flow to pick up color names from a product tag and register multiple color names

Solved

How to use Shopify Flow to pick up color names from a product tag and register multiple color names

nnaga
Tourist
8 1 0

I am new to Shopify Flow and am having difficulty implementing it on my own.

What I want to do is to use Shopify Flow to pick up a color name that is already registered as a tag on a product and register the corresponding color name in a metafield.
For example, if a product is available in ivory and gray, I would like to register ivory and gray in the meta field as well.
I have already created a workflow and succeeded in picking up the product tag "ivory" and registering "ivory" in the color meta field, but I don't know how to register multiple colors.

I also have a different pattern.
A new gray color is added to a product that is already registered as ivory.
If there is a way to add the gray color to the ivory color without changing the ivory color, please let us know.
I tried simply creating another workflow, but it overwrote the existing color.

I did my own search but could not find the answer successfully.
If you have found a similar post in the past, I would appreciate it if you could let me know.
Thank you in advance.

 

 

Attached is a successful workflow with a single color name.

無題.png

 

Accepted Solution (1)
paul_n
Shopify Staff
1433 157 332

This is an accepted solution.

You cannot use the tag variable.size in Flow yet. Only variable | size works, but they you can't use it in a comparison. Try this...it also makes it a bit easier to add new colors but you need to watch out for if one color name contains another color name like blue vs indigo blue. 

 

 

{%- assign mf_object = product.metafields | where: "namespace", "custom" | where: "key", "color" | first -%} 
{%- assign mf_str = mf_object.value | replace: "[", "" | replace:"]","" | strip -%} 
{%- if mf_str != blank %}{% assign new_list = mf_str | append: "," %}{% else %}{% assign new_list = "" %}{% endif -%} 
{%- for tag_item in product.tags -%}
{%- if "ivory blue other_color_names" contains tag_item -%}
{%- capture new_list %}{{ new_list }}"{{ tag_item }}",{% endcapture -%}{%- endif -%}
{%- endfor -%}
[{{new_list | split:"," | join:","}}]

 

 

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

Replies 6 (6)

paul_n
Shopify Staff
1433 157 332

Remove the condition. Instead, write some liquid in the Update Product Metafield action to loop over tags looking for conditions and then use it to set the new value. 

 

Something like below. 

 

{%- assign mf_object = product.metafields | where: "namespace", "custom" | where: "key", "color" | first -%}
{%- assign mf_str = mf_object.value | replace: "[", "" | replace:"]","" | strip -%}
[{{mf_str }}
{% for tag_item in product.tags %}
{%- if tag_item == "ivory" -%}
,"ivory"
{%- elsif tag_item == "blue" -%}
,"blue"
{%- 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.
nnaga
Tourist
8 1 0

Thank you very much.
I tried it immediately and it did not work well with an error.

I removed the condition and copied and pasted the code into the Value of the Update Product Metafield, but am I doing it wrong?

 

First image
Tag attached to the product (this is a test product)

 

Second image
Shopify Flow, workflow contents

 

Third image
Error text

 

無題.png

無題2.png

無題3.png

DaveMcV
Shopify Staff
104 31 29

Hi Nnaga,

 

It looks like the problem is related to having an empty array of existing metafields, which means the ",blue" and ",ivory" value will be malformed.

You may need to define the delimiter to be dynamic based on if there are existing entries and by reassigning it as you iterate through the tags by doing something like:

{%- assign mf_object = product.metafields | where: "namespace", "custom" | where: "key", "color" | first -%}
{%- assign mf_str = mf_object.value | replace: "[", "" | replace:"]","" | strip -%}
{%- assign existing_tag_array = mf_str | split: ", " -%}
{%- capture delimeter -%}
{%- if existing_tag_array.size > 0 -%},{%- endif -%}
{%- endcapture -%}
[{{mf_str }}
{% for tag_item in product.tags %}
{%- if tag_item == "ivory" -%}
{{delimeter}}"ivory"
{%- assign delimeter = "," -%}
{%- elsif tag_item == "blue" -%}
{{delimeter}}"blue"
{%- assign delimeter = "," -%}
{%- endif -%}{% endfor %}
]

 

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.
nnaga
Tourist
8 1 0

Hi DaveMcV,

 

As you suggested, I registered the text in the meta-field in advance and then ran Paul_n's workflow and it worked!
Thank you.

 

I continued to try the code DaveMcV gave me, but I got "An internal error occured. Try again later."
Sorry for all the questions, but could you please tell me if I am doing something wrong?

I would like to register in Shopify Flow even if the meta field is blank, so I would like to know how to continue to do this if the field is blank.

 

無題.png

paul_n
Shopify Staff
1433 157 332

This is an accepted solution.

You cannot use the tag variable.size in Flow yet. Only variable | size works, but they you can't use it in a comparison. Try this...it also makes it a bit easier to add new colors but you need to watch out for if one color name contains another color name like blue vs indigo blue. 

 

 

{%- assign mf_object = product.metafields | where: "namespace", "custom" | where: "key", "color" | first -%} 
{%- assign mf_str = mf_object.value | replace: "[", "" | replace:"]","" | strip -%} 
{%- if mf_str != blank %}{% assign new_list = mf_str | append: "," %}{% else %}{% assign new_list = "" %}{% endif -%} 
{%- for tag_item in product.tags -%}
{%- if "ivory blue other_color_names" contains tag_item -%}
{%- capture new_list %}{{ new_list }}"{{ tag_item }}",{% endcapture -%}{%- endif -%}
{%- endfor -%}
[{{new_list | split:"," | join:","}}]

 

 

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.
nnaga
Tourist
8 1 0

Sorry for the delay in confirming.

I got what I wanted to do!
On a test product with several color tags, the color names for the ones with color tags are now registered in the meta field.
I now know how to write the code a little better, so I will study it myself from now on.
Thank you very much!