【问题标题】:Showing a list of top-selling products in a given collection but omitting a specific one?显示给定系列中最畅销的产品列表,但忽略特定产品?
【发布时间】:2018-06-13 22:48:58
【问题描述】:

我希望在产品页面中创建一个部分,在该部分中显示某个集合中最畅销的产品,但省略显示该产品页面的产品页面。

到目前为止,除了默认排序方式之外,我没有看到任何对集合对象进行排序的选项。同样,我看不到从返回的结果中省略给定 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 %}

这只是返回集合somecollection 的前四个产品。

我无法想象这是一个不常见的用例,所以有人知道像SORTBY sales OMIT product.id 这样的方法吗(当然是伪代码,但就是这样)。

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    您需要将收藏的排序顺序设置为“畅销”。

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

    此外,您必须将限制更改为 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 %}
    

    【讨论】:

    • 谢谢!两个快速修复 - 您的代码始终输出五个项目。比较应该在 id 和 id 之间进行,因为 id 和 handle 总是不同的,并且它应该在这两种情况下停止输出,所以我将 and 更改为 or
    • @AK 是的,你是对的。没有检查它是否有我在工作的错误。 :D 我更新了代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多