Cannot get fairly straightforward if / else to output correctly.

Windsinger
Tourist
11 0 1

Hello.

I currently have the below liquid working on my site.

Ideally I would like the output to read "50% OFF" (if a 'Closeout' tag and a 'Closeout __%' tag are present)  OR "Sale!".

Unfortunately I've had to compromise with "50% OFF Sale!" or "Sale!" because I cannot get an else statement to work. I either only get "Sale!" or I get "Sale!Sale!Sale!Sale!..." repeated and sometimes "50% OFF" at the end. Moving the {% break %} around hasn't solved it either. 

{% if product.compare_at_price %}
<span class="badge sale sale-sticker" style="display: initial; margin-top: 0px;">{% for tag in product.tags %}{% if tag contains 'Closeout' and tag contains '%' %}{{ tag | remove: "Closeout " }} OFF {% break %}{% endif %}{% endfor %}Sale!</span>
{% endif %}

Thanks in advance for any direction. Seems like it should be easier to get working.

Side note: I have both a "Closeout" and "Closeout 50%" tag on products because I can only create a collection with tag equals (not tag contains....)

0 Likes
Windsinger
Tourist
11 0 1

I ended up taking another tack, which worked. Leaving this here for any one having similar issue.

{% if product.compare_at_price %}
{% assign closeout_flag = false %}
{% for t in product.tags %}
{% if t contains "Closeout" %}
{% assign closeout_flag = true %}
{% break %}
{% endif %}
{% endfor %}  
<span class="badge sale sale-sticker" style="display: initial; margin-top: 0px;">{% unless closeout_flag %}Sale!
{% else %}  
{% for t in product.tags %}
{% if t contains "Closeout" and t contains "%" %}
      {{ t | remove: "Closeout " }} OFF 
{% endif %}
{% endfor %}  
{% endunless %}
{% endif %}
0 Likes