Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Need help on how to code conditional snippets so that the snippet is only displayed if the product is contained in a certain Collection.
What I want to accomplish is to have a snippet included on specific Products if they are included in a specific Collection.
I found the product.collection object documentation, and I have already implemented some conditional snippets using the product.type object, but not sure how to use the product.collection object in that code:
{% for collection in product.collections %}
{{ collection.title }}
{% endfor %}
{% if product.type == 'T-Shirt' %}
{% include 'product-t_shirt' %}
{% elsif product.type == 'Sticker'%}
{% include 'product-sticker' %}
{% endif %}
Would something like the following be the proper way to do this?
{% for collection in product.collections %}
{% if collection.title contains 'My_Stickers' and collection.title contains 'circle stickers' %}
{% include 'product-circle_stickers' %}
{% elsif collection.title contains 'My_Stickers' and collection.title contains 'square stickers' %}
{% include 'product-square_stickers' %}
{% endif %}
{% endfor %}
OK I just tested this and it seems to be working as intended.
A great tip for everyone is seek for this solution is use {% break %} to exit a for loop. Once you've discovered one 'new' tag, you can exit your loop.
{% for tag in product.tags %}
{% if tag contains 'new' %}
<div class="new-tag new-tag--absolute"> NEW </div>
{% break %}
{% endif %}
{% endfor %}
Credit for user toastifer from a answer on StackOverflow.
----------------------
PT-BR
Uma dica pra quem esta usando esse tipo de código como exemplo é colocar a tag {%break%} dentro do loop para parar o loop quando encontrar a tag desejada. Só usar o código de exemplo citado acima.
Another way this can be done is by the use of the where filter and checking for the handle of the collection ,which is more relaiable than the title because this last could change eventually whereas the handle will always remain the same
{% liquid
assign circle_stickers = product.collections | where: 'handle' : '[circle-handle]
assign square_stickers = product.collections | where: 'handle' : '[square-handle]
if circle_stickers != blank
include 'product-circle_stickers'
elsif square_stickers != blank
include 'product-square_stickers'
endif
%}
Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025