Update metafield value with another metafield value.

Solved

Update metafield value with another metafield value.

UnlockHomes
Tourist
12 0 2

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
Tourist
12 0 2

This worked perfectly! Thank you!

paul_n
Shopify Staff
1336 151 306

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
Tourist
12 0 2

Hi, just tried it. Worked perfectly, thanks!