Re: How to isolate first of metafield values

Solved

How to extract the first value from a list type metafield?

jake_mitchell
Shopify Partner
120 2 63

Hi, 

I'm trying to set up a flow that sends a HTTP request to add the value in a metafield (prods.category) to productType

 

This is what I have so far:

{% for product_mf in product.metafields %}
{% if product_mf.namespace == "prods" and product_mf.key == "category" %}
{% assign category_info = product_mf.value %}
{% endif %}
{% endfor %}

{% assign final_value = category_info | remove: "[" | remove: "]" | remove: '"' | strip %}

{
"query": "mutation { productUpdate(input: {id: \"{{ product.id }}\", productType: \"{{ final_value }}\"}) { product { id } } }"
}

It works perfectly if there is only one value in prods.category, but it is a list type metafield and will sometimes have multiple values. I want to only take the first value. 

However, at the moment it will put both in. 

 

As Flow is limited in terms of what liquid tags can be used and there isn't really a way of replicating how the code works anywhere else I'm a bit at a loss. 


The AC on this are: 

Given product A has 'Value A' & 'Value B' in product.metafields.prods.category

When the flow is run

Then only 'Value A' should be added to productType via the HTTP request

 

I've tried splitting, slicing, first and a few other ways but have hit a wall. 

 

Thanks

Accepted Solution (1)

paul_n
Shopify Staff
1366 153 321

This is an accepted solution.

I think you are saying that the metafield is a list of single line text. You might see the following

# Case 1 - null
# Case 2 - empty list
[]
# Case 3 - single value
["foo"]
Case 4 - multiple values
["foo","bar"]

For final value, instead of:

{% assign final_value = category_info | remove: "[" | remove: "]" | remove: '"' | strip %}

Try: 

{% assign final_value_list = category_info | remove: "[" | remove: "]" | split:"," %}
{% for final_value in final_value_list %}{% if forloop.first %}{{ final_value | strip | remove: '"' }}{% endif %}{% endfor %}

This handles case 3 and 4.

 

Also you probably want to add some hyphens to your tags to strip out new lines around your api call

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.

View solution in original post

Replies 5 (5)

paul_n
Shopify Staff
1366 153 321

This is an accepted solution.

I think you are saying that the metafield is a list of single line text. You might see the following

# Case 1 - null
# Case 2 - empty list
[]
# Case 3 - single value
["foo"]
Case 4 - multiple values
["foo","bar"]

For final value, instead of:

{% assign final_value = category_info | remove: "[" | remove: "]" | remove: '"' | strip %}

Try: 

{% assign final_value_list = category_info | remove: "[" | remove: "]" | split:"," %}
{% for final_value in final_value_list %}{% if forloop.first %}{{ final_value | strip | remove: '"' }}{% endif %}{% endfor %}

This handles case 3 and 4.

 

Also you probably want to add some hyphens to your tags to strip out new lines around your api call

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.
wonena-dev
Shopify Partner
21 0 8

I couldn't make it work with this as a solution. 

where did you declare the final_value? I only saw the final_value_list

 

 

Building a Sustainable Marketplace from Scratch
paul_n
Shopify Staff
1366 153 321

It's in the "for" loop. FYI, Flow now has a much better way of accessing metafields.

 

This part is no longer necessary:

{% for product_mf in product.metafields %}
{% if product_mf.namespace == "prods" and product_mf.key == "category" %}
{% assign category_info = product_mf.value %}
{% endif %}
{% endfor %}

It can replaced with something like the below, assuming metafield name is Category Info:

{{ product.categoryInfo.value }}

 Also, I'd recommend using Run code to work with lists. It is way easier to work with lists in JavaScript. We have some example code here that converts tags to a metafield list: https://github.com/Shopify/flow-code-examples/blob/main/run-code-examples/product-tags-to-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.
wonena-dev
Shopify Partner
21 0 8

By metafield name, do you mean key, or name with spaces? In my case....

 

CEOWonena_0-1726173455671.png

 

Building a Sustainable Marketplace from Scratch
paul_n
Shopify Staff
1366 153 321

You shouldn't need to know that. Click "Add a variable", choose "product" and then "metafield (requires arguments)". Choose your metafield...it will insert the liquid for you

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.