list - How to display products from z to a in a liquid template in Shopify
how to list the products in reverse order (i.e. from Z to A) in a liquid template?
Thanks for your help !
Here is my code but it doesn't work:
{% assign myproducts = collections.all.products | sort='title' | reverse %}
{% paginate myproducts by 1000 %}
{% for product in myproducts %}
{% include 'product-thumb' with size:'oneThird', thumbSize:'thirds' %}
{% endfor %}
{% endpaginate %}
Answer
Solution:
There are few syntax error in your code, but the main problem is that the pagination will not work (and there is no way to make it work).
{% assign myproducts = collections.all.products | sort: 'title' | reverse %}
{% for product in myproducts %}
{{ product.title }}
{% endfor %}
This will display the product in reverse order but without pagination.
Pagination doesn't work with assigned variables but only with collections. You could write something like this
{% paginate collections.all.products by 3 %}
{% for product in collections.all.products %}
{{ product.title }}
{% endfor %}
{% endpaginate %}
But that's not reversed. And you can't reverse the collection before paginating. (You can reverse the single page, but that's not very useful).
A solution is to create your own manual collection, with the order you prefer (you set it on the shopify backend) and display that paginated.
To do that:
Go to your-shop .myshopify.com/admin/collections -> Create Collection -> You set a title -> You set a condition (could be price >0) -> Save. You can now sort it by title z-a.
Then in your code you can put
{% assign myproducts = collections['your-title'].products %}
{% for product in myproducts %}
{{ product.title }}
{% endfor %}
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.