Ad

Shopify Outputs Multiple Tags When Looping

- 1 answer

I have a product with about 10 tags and I'm trying to check if a product tag equals 'OurPrice' and if it does display Our Price otherwise display Sale. Im using this code:

{% for tag in product.tags %}
 {% if tag == 'OurPrice' %}Our Price:
 {% else %}Sale:
 {% endif %}
{% endfor %}

I'm only wanting it to display one or the other, currently i'm getting Sale:Sale:Sale:Sale:Sale:Sale:Sale:Sale:Sale:Sale:Sale:Our Price:Sale:

Is there a way to fall check for this?

EDIT: I now have:

{% assign isOurPrice = False  %}
{% for tag in product.tags  %}
 {% if tag == 'OurPrice' %}Our Price:{% assign isOurPrice = True  %}
 {% endif %}
{% endfor %}
{% if isOurPrice == False %}Sale:
{% endif %}

which displays Our Price: Sale: which is displaying Sale when it shouldn't. Any ideas?

Ad

Answer

Turns out shopify offer code for this - 'contains'

{% if product.tags contains "OurPrice" %}
  Our Price:
{% else %}
  Sale:
{% endif %}
Ad
source: stackoverflow.com
Ad