Reputation: 27
I want to sort collection products by 'best-selling' but it's not work when i test .
i don't know why. Below is my code
{% assign products = collection.products | sort: 'best-selling' %}
When i sort it with 'price', it work, but with best-selling it's not.I hope someone can help me how to do it
Upvotes: 1
Views: 2832
Reputation: 56
Using Shopify Admin:
Sort order
to Best selling
from the dropdown menu.This change will automatically sort the products in that collection based on their sales volume.
In Your Liquid Template:
{% for product in collection.products %}
<!-- Display product details -->
{% endfor %}
This setup respects the sorting order set via the Shopify admin and displays products sorted by best-selling in your store.
Upvotes: 0
Reputation: 1965
The sort
function is a generic sort for array. You can sort by any field of the element in the array. In case of products you can write
{% assign products = collection.products | sort: 'title' %}
because the title (or price, etc) is an attribute of the product. A product doesn't have any attribute that specify how many units have been sold.
You can change the default sorting method of your collection to 'Best Selling' so that products are sorted that way by default.
Also, you were probably confused by the sort_by
function which is quite different
{{ collection.url | sort_by: 'best-selling' }}
gives you the link to the collection page using the given sort function.
Upvotes: 1