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

Shopify站点product-badge.liquid折扣百分比计算错误求助

问题根因

折扣百分比计算逻辑存在两处错误:

  • 多执行了一次times: 100操作:(对比价-售价)*100/对比价已经是百分比数值,再次乘100会导致结果放大100倍,和你效果图中异常大的折扣数值完全吻合
  • 计算过程中错误使用money_without_currency做格式化:该过滤器会把数值转为带格式的字符串,提前转换会干扰后续数值计算精度

修复后代码

直接替换product-badge.liquid中对应的代码即可:

{% if sold_out %}
<span class="soldout-title">{{ settings.soldout_title }}</span>
{% else %}
{% if on_sale %}
{% if settings.sale_title != '' %}
<span class="sale-title">{{ settings.sale_title }}</span>
{% endif %}
{% if settings.sale_percent_enable %}
{% assign discount_percent = product.selected_or_first_available_variant.compare_at_price | minus: product.selected_or_first_available_variant.price | times: 100.0 | divided_by: product.selected_or_first_available_variant.compare_at_price | round %}
<span class="percent-count">-{{ discount_percent }}%</span>
{% endif %}
{% endif %}
{% endif %}

验证规则

替换后可按以下逻辑验证计算正确性:

  • 商品对比价100元,售价70元,应展示-30%
  • 商品对比价299元,售价159元,应展示-47%

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 12:36:04