Display the list of product's collections on a product page

Hello!
Imagine I have a product that belongs to more than one collection. I’d like to have the list of the product’s collections displayed in the product description, so that users can quickly click on one of the collections to explore further.
To display the list of product TAGS, I’m using the following code:

{% if product.tags.size > 0 %}
Tags: {% for tag in product.tags %}
{% assign tag_coll = ‘/collections/all/’ | append: tag %}
{{ tag | capitalize | link_to: tag_coll }}{% unless forloop.last %},{% endunless %}
{% endfor %}
{% endif %}

How should I amend it to display the list of COLLECTIONS that a product belongs to? For example, this code from @Niraj_singh displays the first colelction in the list, but how to display the entire list?
{% assign pro_collection = product.collections.first %}
{% if pro_collection %}
This product is part of my {{ pro_collection.title | link_to: pro_collection.url }} Collection
{% endif %}

Any advice much appreciated!

1 Like

Hi,

This link may answer your question.

https://shopify.dev/api/liquid/objects/product

This product belongs in the following collections:
{% for collection in product.collections %}
{{ collection.title }}
{% endfor %}

1 Like

Fantastic! Thank you David. And here’s the amended code to diplay the list of collections as links (just in case someone like me needs it):

Collections: {% for collection in product.collections %} {% assign coll = '/collections/all/' | append: collection.title %} {{ collection.title | capitalize | link_to: coll }}{% unless forloop.last %},{% endunless %} {% endfor %}

2 Likes

UPDATED:
The previous solution worked, but I found out that it didn’t work for 2-word collections, like “board games”: the collection link turns spaces to “-”, while the code suggested turns spaces into “%20”. What we need is a short addition that replaces spaces with “-”, too:
IF YOU NEED THE COLLECTIONS LISTED WITH COMMAS, USE:

{% for collection in product.collections %} {% assign coll = '/collections/' | append: collection.title | replace: " ", "-" %} {{ collection.title | capitalize | link_to: coll }}{% unless forloop.last %},{% endunless %} {% endfor %}

IF IT’S OK WITHOUT COMMAS, USE SHORTER OPTION:

{% for collection in product.collections %} {{- collection.title | link_to: collection.url }} {% endfor %}

2 Likes

How can I make sure not to display collections that are in status: DRAFT ?