Reputation: 8882
I'm looking to create a section of a product page where I show the top selling products in a certain collection, but omitting the one whose product page this is displayed on.
As of right now, I do not see any option to sort the collections object aside from the way it is sorted by default. Likewise, I do not see a way to omit a given product.id
from the returned results.
Currently, my code looks like this:
{% for top_product in collections['somecollection'].products limit:4 %}
<div class="top-seller-item">
<img src="{{ top_product.images[0] | img_url: '1024x1024' }}"/>
<a href="{{ top_product.url }}">{{ top_product.title }}</a>
</div>
{% endfor %}
This just returns the first four products of the collection somecollection
.
I can't imagine this is an uncommon use case, so does anyone know a way to do something like SORTBY sales OMIT product.id
(pseudocode, of course, but that's the idea).
Upvotes: 0
Views: 1427
Reputation: 12943
You will need to set the sorting order of the collection to be by "Best Selling".
After that you will need to create a counter variable that will count the unique products in your loop.
In addition you have to change the limit to 5 so that you get 4 elements even if the product is present in the first 4 products.
In code it will look something like this:
{%- assign counter = 0 -%}
{% for top_product in collections['somecollection'].products limit:5 %}
{%- unless top_product.id == product.id or counter >= 4 -%}
{%- assign counter = counter | plus: 1 -%}
<div class="top-seller-item">
<img src="{{ top_product.images[0] | img_url: '1024x1024' }}"/>
<a href="{{ top_product.url }}">{{ top_product.title }}</a>
</div>
{%- endunless -%}
{% endfor %}
Upvotes: 1