Solved

Update metafield value with another metafield value.

UnlockHomes
New Member
4 0 0

Hi all,

 

For my products, I have a metafield that defines length (.custom.length), and one that defines shape(.custom.shape). 

 

I am trying to create a flow, where when triggered, updates the cutline metafield (theme.cutline) to their length and shape values. All three metafields are single lined texts. (So for example length value = Medium and shape value = Oval, cutline value with be "Medium Oval"

 

After research I see that for liquid I need to do something like this:

{% for metafields_item in product.metafields %}
  {% if metafields_item.namespace == "custom" and metafields_item.key == "length" %}
    {{metafields_item.value}}
  {% endif %}
{% endfor %}

I've also tried this:

{% for metafields_item in product.metafields %}
  {% if metafields_item.namespace == "custom" and metafields_item.key == "length" %}
    {% assign length_value =  metafields_item.value %}
  {% endif %}
{% endfor %}

{{ length_value }}

But both returns this when run:

Got error updating metafield: "Value must be a single line text string." For value: "\n \n Medium\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n\n", Type: single_line_text_field

Is there any way I can fix this? Thank you!

Accepted Solution (1)

TDogs
Tourist
4 1 2

This is an accepted solution.

Try using hyphens to eliminate the newlines:

 

{%- for metafields_item in product.metafields -%}
  {%- if metafields_item.namespace == "custom" and metafields_item.key == "length" -%}
    {{- metafields_item.value -}}
  {%- endif -%}
{%- endfor -%}

View solution in original post

Replies 4 (4)

TDogs
Tourist
4 1 2

This is an accepted solution.

Try using hyphens to eliminate the newlines:

 

{%- for metafields_item in product.metafields -%}
  {%- if metafields_item.namespace == "custom" and metafields_item.key == "length" -%}
    {{- metafields_item.value -}}
  {%- endif -%}
{%- endfor -%}
UnlockHomes
New Member
4 0 0

This worked perfectly! Thank you!

paul_n
Shopify Staff
1005 133 240

Flow just launched improved metafield support, which makes it easier to find and use metafield data in Flow: https://changelog.shopify.com/posts/shopify-flow-access-typed-metafields

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.
UnlockHomes
New Member
4 0 0

Hi, just tried it. Worked perfectly, thanks!