【问题标题】:Loop through products in collection and find lowest price (excluding $0 prices)循环浏览收藏中的产品并找到最低价格(不包括 0 美元的价格)
【发布时间】:2018-03-29 01:36:09
【问题描述】:

我有一个包含多个类别的 Shopify 网站。有些产品没有价格(Shopify 对此感兴趣为 0 美元)。所有收藏都显示在首页上。我想遍历每个集合中的所有产品并输出最低价格(不包括 $0 值)。

我正在使用的当前数组是:

{% assign startingFrom = collection.products | sort: 'price' %}
{{ startingFrom[0].price_min | money_without_currency }}

不幸的是,这会捕获并输出 $0 值。

遍历高于 0 美元的值并输出最低价格的最佳方法是什么?

我已尝试排除零值:

{% assign startingFrom = collection.products | sort: 'price' %}
{% if 'price' != 0.00 %}
 {{ startingFrom[0].price_min | money_without_currency }}
{% endif %}

这会输出 0.00 美元。

【问题讨论】:

    标签: arrays shopify liquid


    【解决方案1】:
    {% for product in collection.products %}
      {% if forloop.first %}
        {% assign lowest_price = product.price %}
      {% endif %}
      {% assign new_price = product.price %}
      {% if new_price < lowest_price and new_price != 0 %}
        {% assign lowest_price = new_price %}
      {% endif %}
    {% endfor %}
    

    它没有经过测试,但应该可以工作。

    【讨论】:

      【解决方案2】:

      您不能依赖这种方式,除非您的收藏中的产品小于页面大小限制。

      查看Shopify: Getting highest price item from collection下的讨论和解决方案

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-05
        • 2015-09-16
        • 2018-04-26
        • 2016-05-08
        • 2016-05-09
        • 1970-01-01
        • 2020-10-29
        相关资源
        最近更新 更多