Shopify If in collection then display this
I am trying to write a simple if statement but am always struggling with the shopify system.
Essentially, I want this to be done:
{% if collection.product == 'discontinued'%} This product has been discontinued. {% endif%}
If this collection displays this text / html. Otherwise, nothing will be displayed. This will be in the product.liquid template.
Any ideas?
source to share
If I understand how liquid collections work in Shopify, you would need to iterate over all of your products.
You need to do something similar if you work with collections directly:
{% for product in collection.product %}
{% if product.tags contains 'discontinued' %}
This product has been discontinued :(
{% endif %}
{% endfor %}
If you're just working with one product, you can probably just use the inside of the tag if
.
Literature:
source to share
You can actually add discontinued products to a collection called discontinued.
When rendering a product, you can do, as csaunders suggests, simply loop through all products in a collection with discontinuation and check if the current product ID matches any of the products in that collection. If so, do what you have to do. There is no need to use tags.
source to share
I think it will help anyone, I used shopify in the sidebar of the site. The current collection page will be checked by this below code.
<div class="row-fluid not-animated" data-animate="fadeInUp">
<div class="title">By Collections</div>
<form class="coll">
{% assign col_tags = collection.title %}
{% for collection in collections %}
<input type="radio" value="{{ collection.url }}" name="collections" {% if col_tags contains collection.title %} checked {% endif %} >{{ collection.title | escape }} <br/>
{% endfor %}
</form>
source to share