Multiple if statements when adding metafield value to tags

Hi,

We have a metafield that we use when listing new items.

The values in the metafields are used to create tags, sometimes with prefixes for filtering.

This is what it looks like now:

The code in the action is;

{% for mf in product.metafields %} {% if mf.namespace == "product" and mf.key == "genre_books" %} Genre: {{ mf.value | replace: ",", ", Genre: " | remove: "[" | remove: "]" | remove: '"' }} {% endif %}{% endfor %}

The action here adds 'Genre: ’ to the start of the tag.

I would like to exclude certain metafield values from that prefixing rule - those values are ‘Fiction’ and ‘Non-Fiction’

I have tried to do this in two ways so far:

Neither of them give the desired result and we will see tags generated for Fiction and Non-Fiction with the prefix of 'Genre: ’ which shouldn’t be happening.

My question: is there a way to write this logic into the liquid, so that

  • if metafield value is Fiction or Non Fiction add tag without prefix
  • if metafield value is not one of Fiction or Non Fiction add tag with prefix

Thanks

1 Like

Here’s some other unsuccessful attempts:

V3

{% for mf in product.metafields %}
{% if mf.namespace == “product” and mf.key == “genre_books” and mf.value == “Fiction” %}
{{ mf.value | remove: “[” | remove: “]” | remove: ‘"’ }}
{% elsif mf.namespace == “product” and mf.key == “genre_books” and mf.value == “Non-Fiction” %}
{{ mf.value | remove: “[” | remove: “]” | remove: ‘"’ }}
{% elsif mf.namespace == “product” and mf.key == “genre_books” %}
Genre: {{ mf.value | replace: “,”, “, Genre: " | remove: “[” | remove: “]” | remove: '”’ }}
{% endif %}
{% endfor %}

V4

{% for mf in product.metafields %}
{% if mf.namespace == “product” and mf.key == “genre_books” and mf.value == “Fiction” %}
Fiction
{% elsif mf.namespace == “product” and mf.key == “genre_books” and mf.value == “Non-Fiction” %}
Non-Fiction
{% elsif mf.namespace == “product” and mf.key == “genre_books” and mf.value != “Fiction” and mf.value != “Non-Fiction” %}
Genre: {{ mf.value | replace: “,”, “, Genre: " | remove: “[” | remove: “]” | remove: '”’ }}
{% endif %}
{% endfor %}

V5

{% for mf in product.metafields %}
{% if mf.namespace == “product” and mf.key == “genre_books” and mf.value contains “Fiction” %}
Fiction
{% elsif mf.namespace == “product” and mf.key == “genre_books” and mf.value contains “Non-Fiction” %}
Non-Fiction
{% elsif %}
{% elsif mf.namespace == “product” and mf.key == “genre_books” and mf.value != “Fiction” and mf.value != “Non-Fiction” %}
Genre: {{ mf.value | replace: “,”, “, Genre: " | remove: “[” | remove: “]” | remove: '”’ }}
{% endif %}
{% endfor %}

Hi Jake,

Here is a liquid sample I made that I think achieves the behaviour you’re looking for:

{% assign genres = '["Fiction", "Non-Fiction", "Comedy"]'| remove: "[" | remove: "]" | remove: '"' | split: ", " %}
{% for genre in genres %}{% if genre contains "Fiction" or genre contains "Non-Fiction" %}{{ genre }}{% else %}Genre: {{ genre }}{% endif %}, {% endfor %}

I was able to add this liquid to the “add products tag” action and got the following:

This should work if you substitute my fake list with the value of your metafield.