621 votes
1 answers
shopify - How to search for a value in a tag that is stored as "key:value"
Solution:
So:
{% for tag in product.tags %}
{% assign tag_array = tag | split:':' %}
{% if tag_array.last == 'myvalue' %}
Do something
{% else %}
{% continue %}
{% endif %}
{% endfor %}
Explanation: first you loop through tags to get tag string value. Then you split tag to get an array, then you retrieve the value and use it in you conditional statement.
Undefined asked
589
votes
Answer
Solution:
Similar answer to @Alice Girard, I would usecase
andwhen
instead because of the amount of tags you need to define.
{% for tag in product.tags %}
{% assign tag_array = tag | split: ":" %}
{% case tag_array.first %}
{% when 'value1' %}
do something
{% when 'value2' %}
do something
{% else %}
default error message
{% endcase %}
{% case tag_array.last %}
{% when 'abc' %}
do something
{% when 'def' %}
do something
{% else %}
default error message
{% endcase %}
{% endfor %}
Undefined answered
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.