Conditional snippet if product is contained in a Collection?

Conditional snippet if product is contained in a Collection?

George_C
Trailblazer
166 2 152

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 %}
Replies 3 (3)

George_C
Trailblazer
166 2 152

OK I just tested this and it seems to be working as intended. 

DabsDesign
Shopify Partner
49 1 11

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.

 

DABS Design - Sua Agência Shopify no Brasil. Somos especialistas em Shopify e Shopify Plus, oferecendo consultoria, suporte, criação, integração de Apps e desenvolvimento para sua loja virtual. Conte com uma agência Shopify Expert Partner para potencializar o seu negócio! Entre em contato conosco. Transforme sua loja com os especialistas em Shopify no Brasil!

SantiagoCirco
Shopify Partner
9 1 1

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
%}