Solved

Update variant metafield value using another variant metafield value from the same variuant

MarkMcC
Shopify Partner
2 1 0

Hi,

I am trying to understand the code to add to the value text box of the update product variant metafield action step in a flow.

I want the flow to update a variant metafield with the value from another metafield from the same variant if it has a value.

I am trying to repurpose the shopify code example for listing the colours:

{% capture mf_value %}
{%- for tags_item in product.tags -%}
{%- if tags_item contains "color:" -%}
"{{- tags_item | remove_first: "color:" | strip -}}",
{%- endif -%}
{%- endfor -%}
{% endcapture -%}
[{{mf_value | remove_last: ","}}]

 

by changing it to:

 

{% capture mf_value %}
{%- for metafields_item in productVariant.metafields -%}
{%- if metafields_item.key contains "sell" -%}
"{{metafields_item.value}}"
{%- endif -%}
{%- endfor -%}
{% endcapture -%}

 

In my scenario I have a variant metafield with namespace "cross" and key "sell". If this metafield has text in it I want to pull that value into the variant metafield I am updating with the flow action.

I am currently getting the following error when the flow runs of:

Got error updating metafield: "Value can't be blank." For value: "", Type: single_line_text_field

 

Can anyone help?

 

Many thanks

 

Mark

 

 

Accepted Solution (1)

MarkMcC
Shopify Partner
2 1 0

This is an accepted solution.

Update:

 

I have worked this out using the other example for sending a metafield in an email:

{% assign hold_note = order.metafields | where: "namespace", "custom" | where: "key", "hold_note" | first %}
The order has a hold note of: {{ hold_note.value }}

 

I tweaked this to be:

 

{% assign hold_note = productVariant.metafields | where: "namespace", "cross" | where: "key", "sell" | first %}{{ hold_note.value }}

 

also needed to remember that if you have any line breaks in the value text box then you will get a gotcha \n error which you'll then spend time trying to remove, replace, strip etc, whereas you just need to keep your code on one single line without any returns.

View solution in original post

Replies 2 (2)

MarkMcC
Shopify Partner
2 1 0

This is an accepted solution.

Update:

 

I have worked this out using the other example for sending a metafield in an email:

{% assign hold_note = order.metafields | where: "namespace", "custom" | where: "key", "hold_note" | first %}
The order has a hold note of: {{ hold_note.value }}

 

I tweaked this to be:

 

{% assign hold_note = productVariant.metafields | where: "namespace", "cross" | where: "key", "sell" | first %}{{ hold_note.value }}

 

also needed to remember that if you have any line breaks in the value text box then you will get a gotcha \n error which you'll then spend time trying to remove, replace, strip etc, whereas you just need to keep your code on one single line without any returns.

DaveMcV
Shopify Staff
88 28 25

Hi MarkMcC

 

Glad you were able to get that sorted out. One thing to keep in mind with Liquid is that you can use hyphens on the tags to avoid new lines like this:

{%- assign hold_note = productVariant.metafields | where: "namespace", "cross" | where: "key", "sell" | first -%}
{{ hold_note.value }}

In that example, it should work the same as your single line example.

 

 

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.