skip to Main Content

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.

3

Answers


  1. 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 href="{{ product.url | within: collection }}">Details</a>
    
    {% endfor %}
    

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

    Login or Signup to reply.
  2. Try it if your URL in this format “/collections/COLLECTION_HANDLE/products/PRODUCT_HANDLE”

    Try This:
    This product is in the {{ collection.title }} Collection.

    Login or Signup to reply.
  3. Although there might be many collections product assigned but the better way to get the current collection title that is in URL

    Note: Using this code you can do stuff on your product page only if collection is in the URL.

    {% assign product_collection = collection.title | link_to: product_collection.url %}
    {% unless product_collection == blank %}
        <h3>Current Collection is: {{ product_collection }}</h3>
    {% endunless %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search