Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
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
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.
There are a few threads out there on how to do this.
Most recent one:
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 -%}
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: