We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Shopify flow: appending string to existing list of strings metafields

Shopify flow: appending string to existing list of strings metafields

OHDS
Visitor
3 0 0

For update product metafield value sections I'm trying to figure out how to append a string to an existing list. This the code that I currently have:

{% capture mf_value %}
"Red"
{% endcapture -%}
[{{mf_value}}]

 

However this overwrites the existing list while I am trying to append it. 

Replies 3 (3)

paul_n
Shopify Staff
1828 199 434

There are a few threads out there on how to do this. 

Most recent one:

https://community.shopify.com/c/shopify-flow-app/need-help-creating-workflow-need-to-auto-generate-i...

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.
OHDS
Visitor
3 0 0

I tried recreating this but I'm not sure why it's still not appending and overrides it. Here is my code can you have a look please?

 

 

{%- assign current_mf = product.metafields | where: "namespace","filters" | where: "key","colors"| first -%}
{%- assign color_list = current_mf.value | remove:"[" | remove:"]" | strip %}
{%- capture new_color -%}
"Red"
{%- endcapture -%}
{%- if color_list contains new_color -%}
{{ current_mf.value }}
{%- elsif color_list == blank or color_list == "[]" -%}
[{{new_color}}]
{%- else -%}
[{{color_list}},{{new_color}}]
{%- endif -%}

 

Screenshot 2024-01-31 at 4.56.44 PM.png

paul_n
Shopify Staff
1828 199 434

 

 

 

Yesterday Flow release a "Run code" action that makes it possible to write this code in JavaScript instead, which is far easier to get correct. We have an example here for how to convert tags to metafields. Personally, I'd replace your conditions and code with a single Run code step and then this action could be a single "Update product metafield" step with a simple variable in it. 

 

Code example:

https://github.com/Shopify/flow-code-examples/tree/main/run-code-examples/product-tags-to-metafields

 

Template with same use case:

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.