代码之家  ›  专栏  ›  技术社区  ›  SBWT

循环查看集合中的产品并找到最低价格(不包括0美元的价格)

  •  0
  • SBWT  · 技术社区  · 6 年前

    我有一个多类别的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美元。

    2 回复  |  直到 6 年前
        1
  •  0
  •   Sulli Zerance    6 年前
    {% 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
  •  0
  •   bknights    6 年前

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

    请参阅下面的讨论和解决方案 Shopify: Getting highest price item from collection