Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
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
Solved! Go to the solution
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
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
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
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...
By metafield name, do you mean key, or name with spaces? In my case....
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
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024