显示给定系列中最畅销产品的列表但忽略特定产品?

Showing a list of top-selling products in a given collection but omitting a specific one?

我想在产品页面上创建一个部分,在其中显示特定系列中最畅销的产品,但省略了显示该产品的产品页面。

截至目前,除了默认的排序方式外,我没有看到任何对集合对象进行排序的选项。同样,我看不到从返回结果中省略给定 product.id 的方法。

目前,我的代码如下所示:

{% 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 %}

这只是 returns 系列的前四款产品 somecollection

我无法想象这是一个不常见的用例,所以有没有人知道一种方法来做类似 SORTBY sales OMIT product.id 的事情(当然是伪代码,但就是这个想法)。

您需要将集合的排序顺序设置为 "Best Selling"。

之后,您将需要创建一个计数器变量来计算循环中的唯一产品。

此外,您必须将限制更改为 5,这样即使该产品出现在前 4 个产品中,您也可以获得 4 个元素。

在代码中它看起来像这样:

{%- 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 %}