Ad

Displaying The Current Collection On A Product Info Page (shopify Liquid)

- 1 answer

I am on the product info page and would like to know if its possible to get the current collection that the product is in so that i can do some stuff. How can i get the current collection? I have tried collection.title and it doesnt show anything.

Ad

Answer

In order to get the CURRENT collection, you MUST have the collection handle in your URL.

So for example if the product URL is something like so /collections/COLLECTION_HANDLE/products/PRODUCT_HANDLE you will have access to the current collection.

In your case since you don't have access to the collection title I assume that your URL is just /products/PRODUCT_HANDLE.

This means that you are generating the wrong URL's ( not wrong, but not full ). You must look for product.url in your collection and add the filter within: collection.

So your collection liquid code should look something like this

{% for product in collection.products %}

  ... SOME OUTPUT ...
  <a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="{{ product.url | within: collection }}">Details</a>

{% endfor %}

This will force your product url to include the collection URL as well.

Ad
source: stackoverflow.com
Ad