Reputation: 939
I have a collection.list.liquid file.
This loops through ALL the products in the store.
{% for product in collection.products %}
I have created another collection of products that are in a collection cleverly titled 'Special Offers Products', with the url and handle '/special-offers-products'.
Within my FOR loop I want to make an IF statement that simply asks each product within the FOR loop if it is also within this side collection 'Special Offers Products'. If this is TRUE display a little block of code that is essentially a tag that says 'Special Offer!'.
Childishly I have tried (and failed):
{% if product.collections contains 'special-offers-products' %}
<div class="special-offer-banner">
Special<br/>
Offer!
</div>
{% endif %}
I am now struggling to think of another way I could run this IF.
Here is my full product FOR loop:
{% for product in collection.products %}
<div class="single-product">
<a href="{{ product.url | within: collection }}" class="box-link"></a>
<div class="product-image" style="background-image: url({{ product.featured_image.src | img_url: 'large' }});">
</div>
{% if product.collections contains 'special-offers-products' %}
<div class="special-offer-banner">
Special<br/>
Offer!
</div>
{% endif %}
<div class="product-information">
<p class="product-title">{{ product.title }}</p>
<p class="product-vendor">{{ product.vendor }}</p>
<p class="product-price">{{ product.price | money }}</p>
{% unless product.available %}
<br><strong>sold out</strong>
{% endunless %}
<div class="product-buttons">
{% include 'view-button' %}
{% comment %}{% include 'add-to-cart-button' %}{% endcomment %}
</div>
</div>
</div>
{% else %}
<p>no matches</p>
{% endfor %}
How can I to correctly check if the product that is currently being looped over is also placed within another collection?
I do not want to know how to create a separate collection of products using the 'Special Offers Products' collection, this needs to be done within the same ALL products collection loop.
Upvotes: 0
Views: 2829
Reputation: 12943
You were actually very close.
What's confusing you is that product.collections
returns the collections object and not the collection handle.
So you can do this instead.
{%- assign collection_handles = product.collections | map: 'handle' -%}
{% if collection_handles contains 'special-offers-products' %}
<div class="special-offer-banner">
Special<br/>
Offer!
</div>
{% endif %}
This should work with your current logic then.
Upvotes: 5