'case / when' Liquid code - cant get it to work

Solved
Excursionist
41 1 1

So i understand how the code works, but i think my syntax is just wrong. Ive tried different variations but cant seem to get it to work. Im trying to get some basic HTML to show up on a product page when a specific 'product tag' is used for that product. A general image overlay for products that are tagged as "New arrivals, Sale, Clearance, top sellers, etc". Here is the code i have below, but doesnt seem to work.

 

{% case product.tags %}
{% when 'Clearance' %}
<div class="pricebox-clearance-product__tag">
{{ 'products.general.on_clearance' | t }}
</div>
{% when 'Sale' %}
<div class="pricebox-sale-product__tag">
{{ 'products.general.sale' | t }}
</div>
{% when 'Top Seller' %}
<div class="pricebox-top-seller-product__tag">
{{ 'products.general.sale' | t }}
</div>
{% else %}

{% endcase %}

0 Likes
Excursionist
41 1 1

i have a hunch its missing some sort of 'contains' syntax somewhere, which i have tried....but that isnt working either

0 Likes
Shopify Partner
30 0 5

Syntax looks correct to me, not sure why it's not working. I'd recommending just doing:

{% assign tags = product.tags %}
{% if tags contains 'Clearance' %}
<div></div>
{% elsif tags contains 'Sale' %}
etc....
{% endif %}
0 Likes
Excursionist
41 1 1

Adam,

 

I tried the code you used and that does work, but an "if/elseif" only works for 2 variables.....and i have like 5 or more different product tag displays to use up. Which is why im trying to get the 'case/when' to work

0 Likes
Excursionist
41 1 1

This below does not work:

 

{% case product.tags %}
{% when contains 'Clearance' %}
<div class="pricebox-clearance-product__tag">
{{ 'products.general.on_clearance' | t }}
</div>
{% when contains 'Sale' %}
<div class="pricebox-sale-product__tag">
{{ 'products.general.sale' | t }}
</div>
{% when contains 'Top Seller' %}
<div class="pricebox-top-seller-product__tag">
{{ 'products.general.sale' | t }}
</div>
{% else %}

 

 

this below kinda works, but it displays ALL TAGS together...so no, this doesnt work as intended either:

 

{% case product.tags %}
{% when product.tags contains 'Clearance' %}
<div class="pricebox-clearance-product__tag">
{{ 'products.general.on_clearance' | t }}
</div>
{% when product.tags contains 'Sale' %}
<div class="pricebox-sale-product__tag">
{{ 'products.general.sale' | t }}
</div>
{% when product.tags contains 'Top Seller' %}
<div class="pricebox-top-seller-product__tag">
{{ 'products.general.sale' | t }}
</div>
{% else %}

{% endcase %}

{% endcase %}

0 Likes
Highlighted
Shopify Partner
30 0 5

I'm pretty sure that's not correct, you can keep repeating the elsif

0 Likes
Excursionist
41 1 1

 


@Adam_Sanderson wrote:

I'm pretty sure that's not correct, you can keep repeating the elsif



i just tried repeating the 'elseif' for multiple variables, but now nothing at all shows. I think you can only use elseif once.

0 Likes

Success.

Excursionist
41 1 1

actually......no, i stand corrected....you CAN repeat it in a special sequence of order by using multiple elseifs

 

YOU SIR, ARE A GENIUS

 

0 Likes