You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopify站点:遍历各集合产品并排除$0价格获取最低价格

解决Shopify集合中排除$0产品后获取最低价格的问题

你的问题核心是没先过滤掉价格为0的产品,直接排序后自然会把$0的产品排在最前面。这里有两种简洁可靠的解决方案,都能完美满足你的需求:

方案一:先过滤有效产品再排序

这种方法先筛选出所有价格大于0的产品,再对这些产品按价格排序,同时加入空值判断,避免分类全是$0产品时出现报错:

{% comment %} 筛选出最低变体价格大于0的产品(兼容多变体场景) {% endcomment %}
{% assign valid_products = collection.products | where_exp: "product", "product.price_min > 0" %}

{% comment %} 对有效产品按最低价格升序排序 {% endcomment %}
{% assign sorted_products = valid_products | sort: 'price_min' %}

{% comment %} 根据是否有有效产品输出对应内容 {% endcomment %}
{% if sorted_products.size > 0 %}
  {{ sorted_products[0].price_min | money_without_currency }}
{% else %}
  暂无定价
{% endif %}

方案二:提取所有价格后过滤排序

如果你只关心价格数值,也可以直接提取所有产品的最低价格,过滤掉0后再找最小值:

{% comment %} 提取所有产品的最低变体价格 {% endcomment %}
{% assign all_product_prices = collection.products | map: 'price_min' %}

{% comment %} 过滤掉价格为0的项 {% endcomment %}
{% assign valid_prices = all_product_prices | reject: '0' %}

{% comment %} 输出处理结果 {% endcomment %}
{% if valid_prices.size > 0 %}
  {% assign lowest_price = valid_prices | sort | first %}
  {{ lowest_price | money_without_currency }}
{% else %}
  暂无定价
{% endif %}

关键细节说明

  • 为什么用price_min?因为如果产品包含多个变体,price_min会返回该产品的最低变体价格,更贴合你"集合最低价格"的需求;如果你的产品都是单变体,price和price_min效果一致。
  • where_exp是Liquid中支持自定义条件筛选的过滤器,相比普通where,它能处理大于/小于这类比较操作,刚好满足我们排除$0产品的需求。
  • 空值判断不能少:如果某个分类下所有产品都是$0,这段代码会输出友好提示而不是抛出错误。

内容的提问来源于stack exchange,提问作者SBWT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:44:54