If product in collection, show this

New Member
4 0 0

I am looking for the proper code to add an item, such as a link, on a page if the product page open is in a certain collection.

For example: {{ if product in collection.handle == 'accessories' }} This product is an accessory {{ endif }}

This code shows the text no matter what collection the product assigned.

 

Thanks

0 Likes
Shopify Partner
1183 1 64

Hi Adam,

Something like this should work (not tested):

{% assign found = false %}
{% for c in product.collections %}
  {% if c.handle == "secret-sauce" %}
    {% assign found = true %}
  {% endif %}
{% endfor %}

{% if found == true %}
  <h1>You found the secret sauce!</h1>
{% endif %}

Update: now tested.

Gavin.

www.bookthatapp.com ♥ www.searchifyapp.com ♥ www.productsassistant.com
0 Likes
New Member
4 0 0

Thanks Gavin. I was able to get your code working with a couple small tweaks.

  1. Remove the last bit for {% if found %} - not needed - doesn't work
  2. Add what you want to show before the {% endif %} after assigning found to true.
{% assign found = false %}
{% for c in product.collections %}
  {% if c.handle == "collection-name" %}
    {% assign found == true %}
	
		Content you want shown goes here.
	
  {% endif %}
{% endfor %}

 

0 Likes
Shopify Partner
1183 1 64

Ah, that's what you get for not testing it.

I've updated the example so it works. The trick is to do:

{% if found == true %}

 

www.bookthatapp.com ♥ www.searchifyapp.com ♥ www.productsassistant.com
1 Like
New Member
1 0 0

What is the full script for this? Looking for similar answer.

0 Likes