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

如何在商品页面展示自动折扣价与原价并获取折扣比例?

获取自动折扣百分比并展示价格的解决方案

Shopify默认不会在产品页等非购物车/结账页面直接暴露自动折扣的规则信息,要避免手动修改多处代码的麻烦,推荐以下几种方案:

方案1:使用全局元字段存储折扣比例

这是最简单易维护的方式,只需要在后台维护一个值,所有页面统一调用:

  1. 进入Shopify后台 → 设置 → 元字段 → 选择“商店” → 添加定义
    • 名称:自动折扣百分比
    • 命名空间和键:global.autodiscount_percentage
    • 类型:数字(整数或小数,根据需求选择)
  2. 在需要展示价格的模板(如商品页product.liquid、集合页collection.liquid)中调用:
    <!-- 原价展示 -->
    <span class="original-price">{{ product.price | money }}</span>
    <!-- 折扣后价格计算 -->
    {% assign discount_percent = shop.metafields.global.autodiscount_percentage | default: 0 %}
    {% assign discounted_price = product.price | times: (1 - discount_percent | divided_by: 100) %}
    <span class="discounted-price">{{ discounted_price | money }}</span>
    <!-- 折扣比例标签 -->
    {% if discount_percent > 0 %}
    <span class="discount-badge">省{{ discount_percent }}%</span>
    {% endif %}
    
  3. 后续调整折扣时,直接在后台修改该元字段的值即可,无需改动任何代码片段。

方案2:通过主题设置添加自定义配置项

如果需要在主题设置面板里直接调整折扣,可修改主题配置:

  1. 编辑主题代码 → 打开settings_schema.json,在合适的位置添加以下配置(比如放在“定价”分类下):
    {
      "name": "自动折扣设置",
      "settings": [
        {
          "type": "number",
          "id": "autodiscount_percentage",
          "label": "全局自动折扣百分比",
          "min": 0,
          "max": 100,
          "default": 20,
          "info": "设置后将在商品页、集合页等页面自动计算折扣价"
        }
      ]
    }
    
  2. 在模板中调用主题设置值:
    {% assign discount_percent = settings.autodiscount_percentage | default: 0 %}
    <span class="original-price">{{ product.price | money }}</span>
    {% if discount_percent > 0 %}
    <span class="discounted-price">{{ product.price | times: (1 - discount_percent | divided_by: 100) | money }}</span>
    <span class="discount-tag">-{{ discount_percent }}%</span>
    {% endif %}
    
  3. 调整折扣时,进入在线商店 → 主题 → 自定义 → 找到“自动折扣设置”修改数值即可。

方案3:通过API获取自动折扣规则(适合复杂场景)

如果你的自动折扣有多个规则(如特定商品、满减条件),可通过Shopify Admin API获取折扣信息:

  1. 调用PriceRules API端点(GET /admin/api/2024-07/price_rules.json),筛选出类型为automatic的价格规则。
  2. 解析返回的规则数据,提取折扣百分比或金额。
  3. 可以将获取到的折扣信息缓存到主题的元字段中,或者通过前端Ajax请求自己的后端接口来获取(避免直接在前端暴露Admin API密钥)。
  4. 在模板中根据返回的折扣规则,针对不同商品展示对应的折扣价。

注意事项

  • 如果是针对特定商品的自动折扣,建议使用产品元字段(而非全局元字段),为每个商品单独设置折扣比例,避免全局规则冲突。
  • 无论用哪种方案,都要确保展示的折扣和后台设置的自动折扣规则条件一致,避免用户看到的价格与购物车实际结算价格不符。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 10:42:57