In this article, we will show you how to retrieve products in a specific collection using Liquid.
This method is generally used on collection pages (URL → /collections/00), but it is also a major method for displaying collection products on a fixed page, for example.
For example, you may want to display the products in your Christmas collection on a landing page prepared for Christmas, or you may want to display the recommended collections at the bottom of a product detail page! This can also be utilized in cases such as
How to retrieve products within a collection page
The first method is to retrieve products within a collection page (URL → /collections/0000). The product information within a collection is very simple and should already be in the template, but just in case.
It will look something like this
{% for product in collection.products %}
<p>{{product.title}}</p>
{% endfor %}
The products in the collection are retrieved and each product is output. In the above code, the product title is displayed in the p tag.
In addition, there are
- Product image → {{ product.featured_img | img_url }}
- product URL → {{ product.url }}
- Product tags → {{ product.tags }}
- Product price → {{ product.price }}
It is now possible to retrieve elements of the product object, such as
Also, this
{% for product in collection.products %}
is page-separated by 50 products, so if you want to get more than 50 products, you can use the
{% paginate collection.products by 100 %}
{% for product in collection.products %}
<p>{{product.title}}</p>
{% endfor
%}{% endpaginate %}
You need to remove the acquisition limit with pagination, as in the following example. (Note that this method may not be applicable to Shopify's Online Store 2.0 compatible themes.)
How to retrieve products on fixed pages, product detail pages, or anywhere else in your Shopify store
The next step is to process the case where you want to retrieve collection products on a fixed page (URL → /pages/00) or product detail page (URL → /products/00).
To display them in a collection, use the
{% product in collection.products %}
and the products in the collection (products) could be retrieved. However, on fixed pages and product detail pages, it is not possible to specify which "collection" products are to be retrieved.
Therefore, the
{% product in collection.products %}
in Liquid, but from which collection should I pull the products? and Shopify will get lost.
So, pull products from "this collection"! The following is the example of how to do this.
{% product in collections[collection handle name].products %}
This is how it looks like. The collection handle name is found in the Product Management page, under Collections → Collections → Corresponding Collections → Handle Name. (pink frame)
In the above case, "frontpage" is the handle name of the collection.
So, if you want to get a collection on a fixed page or a product detail page and display a list of products in the collection
{% product in collections["frontpage"].products %}
<p>{{ product.title }}</p>
{% endfor %}
The Liquid can be properly retrieved by writing it in the form like the following
How to use [Shopify]Liquid to retrieve information about products in a specific collection.
This is how to retrieve products in a collection with Liquid. The ability to create collections and display them anywhere on the Shopify store is very attractive and easy to use.
And just displaying related products in the category to which the product belongs on the product detail page, for example, can directly lead to sales. I would be happy if I could contribute in any way to the store sales of those who read this.
So, thank you very much for reading to the end! Have a great day!