Collection

From Shopify Wiki

Jump to: navigation, search

The liquid variable collection has the following attributes:

Contents

collection.title

Returns the title of this collection

collection.handle

Returns this collection's handle, which is by default its title in lowercase. Whitespaces in the original title are replaced by dashes in the handle.

The handle of a collection with the title "Winter Sale" would be "winter-sale".

Types and vendors collections don't have a handle.

collection.description

Returns the description of this collection

collection.image

Image which represents the collection. See collection.image.src and collection.image.alt below.

collection.image.src

Returns the relative URL to the collection image. Example: collections/dog-using-keyboard.jpg.

Important: Always use a conditional to check if an image has been uploaded to represent your collection. Also, always use collection.image.src in conjunction with the collection_img_url filter.

Example usage:

{% if collection.image %}
{{ collection.image.src | collection_img_url: 'medium' | img_tag: collection.image.alt | link_to: collection.url }}
{% endif %}

collection.image.alt

Returns the collection title.

collection.products

Returns a collection of all products that are associated with this collection which match the current view. This takes into account things like paginate and selected tags. You can access each product by e.g. a for-iteration:

{% for product in collection.products %}
   {{ product.title }}
{% endfor %}

Note: To filter the products by tags, you can append "?constraint=tagname1+tagname2" to the URL if it's an automatic collection (type or vendor). For any other collection, append: "/tagname1+tagname2", eg. http://shopname.myshopify.com/collections/all/yellow+orange. Also, take note that tags are handleized in the URL, so, if you are filtering with tags 'On Sale' and 'Écru', you will see: http://shopname.myshopify.com/collections/all/on-sale+ecru.

collection.products_count

Returns a count of all of the products in this collection which match the current view. This takes into account the tags currently used to filter the collection.

collection.all_products

Returns the 50 first products that are associated with this collection.

collection.all_products_count

Returns a count of all of the products in this collection.

collection.tags

Returns all tags of all products on in this particular collection which match the current view. This means that if the current view is filtered to only products with a certain tags this variable will hold all the tags these remaining products actually have.

<ul>
  {% for tag in tags %}  
  <li>{{ '+' | link_to_add_tag: tag }} {{ tag | link_to_tag: tag }}</li>
  {% endfor %}
</ul>

Note: if there's no view to speak of (if we are not on a collection page), then this returns the tags of all products in referenced collection.

collection.all_tags

This shows all tags associated with the collection.

All_tags does not seem to currently work with Vision. It's not known when Shopify will update Vision to be compatible with the current live code base.

collection.next_product and collection.previous_product

These methods are available if you scope your product pages to a certain collection. Unclear what that's all about? Read How to Navigate within a Collection.

collection.url

Returns the url for the specific collection.

This is undefined for the automatic 'all' collection, which you should override anyway for many excellent reasons: Taking Control of your Catalog Page , and then you will have a collection that behaves the same as other collections.

This is also undefined for types and vendors collections.

How do you get a collection url then?

{% if collection.handle %}
   {% capture url %}/collections/{{ collection.handle }}{% endcapture %}
   {{ collection.title | link_to: url }}
{% elsif collection.all_products_count > 0 and collection.products.first.type == collection.title %}
   {{ collection.title | link_to_type }}
{% elsif collection.all_products_count > 0 and collection.products.first.vendor == collection.title %}
   {{ collection.title | link_to_vendor }}
{% endif %}

collection.all_types

Returns a list of all unique product types in the collection. File:/upload/e/e0/New.gif

Example usage:

Shop by type:
<ul>
{% for product_type in collection.all_types %}
  <li>
    {{ product_type | link_to_type  }}
  </li>
{% endfor %}
</ul>

See also: shop.types

collection.all_vendors

Returns a list of all unique product vendors in the collection. File:/upload/e/e0/New.gif

Example usage:

Shop by vendor:
<ul>
{% for product_vendor in collection.all_vendors %}
  <li>
    {{ product_vendor | link_to_vendor  }}
  </li>
{% endfor %}
</ul>

See also: shop.vendors