# Collection

The Liquid `collection` object and its properties, with examples.

The `collection` object has the following attributes:

## collection.all_products_count

Returns the total number of products in a collection, even if the collection is filtered.

For the number of products in a filtered collection, use `collection.products_count`.

For example, for a collection with 24 products in total:

```liquid
Total products in this collection: {{ collection.all_products_count }}
```

```text
Total products in this collection: 24
```

## collection.all_tags

Returns a list of all product tags in a collection, even if the collection is filtered. Up to 1,000 tags are returned.

For the tags currently applied, use `collection.tags`.

## collection.all_types

Returns a list of all product types in a collection.

For example, for a collection with products of the types **Accessories**, **Chairs** and **Shoes**:

```liquid
{% for product_type in collection.all_types %}
  {{ product_type | link_to_type }}
{% endfor %}
```

```html
<a href="/collections/types?q=Accessories" title="Accessories">Accessories</a>
<a href="/collections/types?q=Chairs" title="Chairs">Chairs</a>
<a href="/collections/types?q=Shoes" title="Shoes">Shoes</a>
```

## collection.all_vendors

Returns a list of all product vendors in a collection.

For example, for a collection with products of the vendors **VersaCommerce**, **Shirt Company** and **Montezuma**:

```liquid
{% for product_vendor in collection.all_vendors %}
  {{ product_vendor | link_to_vendor }}
{% endfor %}
```

```html
<a href="/collections/vendors?q=VersaCommerce" title="VersaCommerce">VersaCommerce</a>
<a href="/collections/vendors?q=Shirt+Company" title="Shirt Company">Shirt Company</a>
<a href="/collections/vendors?q=Montezuma" title="Montezuma">Montezuma</a>
```

## collection.current_type

You can show products of a given type at the URL `/collections/types` with a query parameter in the format `?q=[type]`, where `[type]` is the product type. For example, this URL returns products of the type **Shirts**:

```text
/collections/types?q=shirts
```

The query value is case-insensitive, so shirts is the same as Shirts or SHIRTS.

On these pages, `collection.current_type` returns the type that was queried. This lets you check whether you are on such a page and output the current type:

```liquid
{% if collection.current_type %}
  Currently viewing {{ collection.current_type | downcase }}.
{% endif %}
```

```text
Currently viewing shirts.
```

## collection.current_vendor

You can show products of a given vendor at the URL `/collections/vendors` with a query parameter in the format `?q=[vendor]`, where `[vendor]` is the product vendor. For example, this URL returns products of the vendor **ApparelCo**:

```text
/collections/vendors?q=apparelco
```

The query is case-insensitive, so apparelco is the same as ApparelCo or APPARELCO.

On these pages, `collection.current_vendor` returns the vendor that was queried. This lets you check whether you are on such a page and output the current vendor:

```liquid
{% if collection.current_vendor %}
  All products by {{ collection.current_vendor }}.
{% endif %}
```

```text
All products by ApparelCo.
```

## collection.default_sort_by

Returns the default sort order of the collection, set on the collection page in the shop admin.

Possible sort orders:

- `manual`
- `best-selling`
- `title-ascending`
- `title-descending`
- `price-ascending`
- `price-descending`
- `created-ascending`
- `created-descending`

## collection.description

Returns the description of the collection.

## collection.featured_image

Returns the featured image of the collection. By default this is the [collection image](#collection-image); otherwise VersaCommerce falls back to a suitable image, for example the image of the first product in the collection.

## collection.filters

Returns an array of [filter objects](https://developer.versacommerce.de/articles/liquid-objects-filter) set up for the collection.

Only filters relevant to the current collection are returned. For collections with more than 1,000 products, the filters are empty.

## collection.handle

Returns the [handle](https://developer.versacommerce.de/articles/liquid-basics-handle) of the collection.

## collection.id

Returns the ID of the collection.

## collection.image

Returns the [image](https://developer.versacommerce.de/articles/liquid-objects-image) of the collection, set on the collection page in the shop admin.

Use the URL filter [image_url](https://developer.versacommerce.de/articles/liquid-filters-url-filters#imageurl) together with the HTML filter [image_tag](https://developer.versacommerce.de/articles/liquid-filters-html-filters#imagetag) to output the image. Check that the image exists before you output it.

For example:

```liquid
{% if collection.image %}
  {{ collection.image | image_url: width: 2048 | image_tag }}
{% endif %}
```

## collection.next_product

Returns the [product object](https://developer.versacommerce.de/articles/liquid-objects-product) of the next product in the collection, or `nil` if there is no next product.

For example, output a link to the next product in the collection:

```liquid
{% if collection.next_product %}
  {{ 'Next product' | link_to: collection.next_product.url, collection.next_product.title }}
{% endif %}
```

## collection.previous_product

Returns the [product object](https://developer.versacommerce.de/articles/liquid-objects-product) of the previous product in the collection, or `nil` if there is no previous product.

For example, output a link to the previous product in the collection:

```liquid
{% if collection.previous_product %}
  {{ 'Previous product' | link_to: collection.previous_product.url, collection.previous_product.title }}
{% endif %}
```

## collection.products

Returns an array of all [products](https://developer.versacommerce.de/articles/liquid-objects-product) in a collection.

Use the [paginate tag](https://developer.versacommerce.de/articles/liquid-tags-theme-tags#paginate) to set how many products are shown per page, up to a limit of 50.

## collection.products_count

Returns the number of products in a collection that match the current view.

For example, if you view a collection filtered by a tag and 6 products have this tag:

```liquid
{{ collection.products_count }} products
```

```text
6 products
```

## collection.published_at

Returns the date and time the collection was published.

Use a [date filter](https://developer.versacommerce.de/articles/liquid-filters-additional-filters#date) to format the date.

## collection.sort_by

Returns the sort order applied to the collection by the `sort_by` URL parameter. Without a `sort_by` URL parameter, the value is `nil`.

For example, for this URL:

```text
/collections/all?sort_by=best-selling
```

```liquid
{% if collection.sort_by %}
  Sort by: {{ collection.sort_by }}
{% endif %}
```

```text
Sort by: best-selling
```

## collection.sort_options

Returns an array of sort options for the collection.

For example:

```liquid
<select id="sort-by">
  {% assign sort_by = collection.sort_by | default: collection.default_sort_by %}
  {% for option in collection.sort_options %}
    <option value="{{ option.value }}" {% if option.value == sort_by %}selected="selected"{% endif %}>{{ option.name }}</option>
  {% endfor %}
</select>
```

```html
<select id="sort-by">
  <option value="manual">Featured</option>
  <option value="best-selling">Best selling</option>
  <option value="title-ascending">Alphabetically, A-Z</option>
  <option value="title-descending">Alphabetically, Z-A</option>
  <option value="price-ascending">Price, low to high</option>
  <option value="price-descending">Price, high to low</option>
  <option value="created-ascending">Date, old to new</option>
  <option value="created-descending">Date, new to old</option>
</select>
```

Each option has these properties:

- option.name
- option.value

## option.name

The customer-facing label of the sort option. Merchants can edit it in the language editor.

## option.value

The value of the sort option that is assigned to the `sort_by` parameter when a form is submitted.

## collection.template_suffix

Returns the name of the custom collection template assigned to the collection, without the `collection.` prefix and the `.liquid` extension. Returns `nil` if no custom template is assigned.

For example, with a collection template named `collection.no-price.liquid`:

```liquid
{% if collection.template_suffix %}
  {{ collection.template_suffix }}
{% endif %}
```

```text
no-price
```

## collection.title

Returns the title of the collection.

## collection.tags

Returns the tags currently applied to a filtered collection, or `nil`.

## collection.url

Returns the URL of the collection.


## More properties

VersaCommerce also provides these properties on the `collection` object:

| Properties | Returns |
|---|---|
| `metafields`, `content`, `full_url`, `active`, `current_tags`, `current_product_index`, `all_products`, `categories`, `categories_count`, `all_categories`, `all_categories_count`, `vendors`, `vendors_count`, `all_vendors_count` | Collection metadata, resource and taxonomy accessors |
