wharry
wharry

Reputation: 31

Get a list of collections for all products in current Shopify collection

We have a primary collection of t-shirts for the product type of clothing (product type is just in case, so, nobody would bring it up). And all of them are also in a few other collections like "linen t-shirts", "silk t-shirts" etc. Let's say i'm on the page of t-shirts collections, and I need to display a list of all the secondary collections like linen, silk and so on. How do I do that? What I've got so far, is I can get a list of collections for each individual product with following code. Further, I'd pull them and sort with js. But I was really hoping for an easier way to get the list.

Here's the liquid code I'm using for each product's collection list:

{% for product in collection.products %}
   {% for collection in product.collections %}
      <div>{{collection.title}}</div>
   {% endfor %}
{% endfor %}

p.s. variants are not an option neither

Upvotes: 2

Views: 13670

Answers (1)

Ruchi
Ruchi

Reputation: 350

On collection page, fetch all the collections. If your current collection handle is t-shirt and your sub collection handle is linen-t-shirt. Check for all the collections which contains current collection handle, then show the details.

{% assign currentCol = collection.handle %}
    {% for collection in collections %}
      {% if collection.handle contains currentCol %}
         {{ collection.title }}
         // further code 
      {% endif %}
    {% endfor %}

Upvotes: 4

Related Questions