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

Shopify Script折扣异常:40%与20%特定客户折扣未生效求助

Shopify客户标签折扣脚本:CIDESCO客户40%/20%折扣不生效问题修复

问题诊断

  1. 未遍历折扣规则数组:DiscountForCustomerTagCampaign#run方法中缺少对@discounts的循环遍历,直接使用未定义的discount变量,导致所有配置的折扣规则均未被实际执行。
  2. 标签匹配逻辑不一致:硬编码的产品标签匹配(如line_item.variant.product.tags.include?("Cidesco-40"))未做大小写和空白字符处理,与CustomerTagSelector、ProductTagSelector类的标准化处理逻辑冲突,可能因标签大小写差异导致匹配失败。
  3. 硬编码折扣规则冗余:单独编写的CIDESCO、学生折扣逻辑与DISCOUNTS_FOR_CUSTOMER_TAG配置数组重复,既增加维护成本,也容易出现逻辑不一致。

修复后的完整代码

DISCOUNTS_FOR_CUSTOMER_TAG = [
  {
    customer_tag_match_type: :include,
    customer_tags: ["Cidesco"],
    discount_type: :percent,
    discount_amount: 10,
    discount_message: "Cidesco atlaide -10% !",
  },
  {
    customer_tag_match_type: :include,
    customer_tags: ["Pastāvīgais klients"],
    discount_type: :percent,
    discount_amount: 10,
    discount_message: "Pastāvīgā klienta atlaide -10% !",
  },
  {
    customer_tag_match_type: :include,
    customer_tags: ["Studentu atlaide instrumentiem"],
    discount_type: :percent,
    discount_amount: 10,
    discount_message: "Studentu atlaide instrumentiem -10% !",
  },
  {
    customer_tag_match_type: :include,
    customer_tags: ["Studentu atlaide instrumentiem"],
    discount_type: :percent,
    discount_amount: 20,
    discount_message: "Studentu atlaide instrumentiem -20% !",
    product_tags: ["Studentu atlaide instrumentiem"],
  },
  {
    customer_tag_match_type: :include,
    customer_tags: ["CIDESCO"],
    discount_type: :percent,
    discount_amount: 40,
    discount_message: "CIDESCO -40% !",
    product_tags: ["Cidesco-40"],
  },
  {
    customer_tag_match_type: :include,
    customer_tags: ["CIDESCO"],
    discount_type: :percent,
    discount_amount: 20,
    discount_message: "CIDESCO -20% !",
    product_tags: ["Cidesco-20"],
  },
]

class CustomerTagSelector
  def initialize(match_type, tags)
    @comparator = match_type == :include ? 'any?' : 'none?'
    @tags = tags.map { |tag| tag.downcase.strip }
  end

  def match?(customer)
    customer_tags = customer.tags.map { |tag| tag.downcase.strip }
    (@tags & customer_tags).send(@comparator)
  end
end

class ProductTagSelector
  def initialize(match_type, tags)
    @comparator = match_type == :include ? 'any?' : 'none?'
    @tags = tags.map { |tag| tag.downcase.strip }
  end

  def match?(product)
    product_tags = product.tags.map { |tag| tag.downcase.strip }
    (@tags & product_tags).send(@comparator)
  end
end

class DiscountApplicator
  def initialize(discount_type, discount_amount, discount_message)
    @discount_type = discount_type
    @discount_message = discount_message
    @discount_amount = if discount_type == :percent
      1 - (discount_amount * 0.01)
    else
      Money.new(pennies: 100) * discount_amount
    end
  end

  def apply(line_item)
    # 仅当未已有折扣时应用
    return if line_item.line_price_was != line_item.line_price

    new_line_price = if @discount_type == :percent
      line_item.line_price * @discount_amount
    else
      [line_item.line_price - (@discount_amount * line_item.quantity), Money.zero].max
    end

    line_item.change_line_price(new_line_price, message: @discount_message)
  end
end

class DiscountForCustomerTagCampaign
  def initialize(discounts)
    @discounts = discounts
  end

  def run(cart)
    return unless cart.customer&.tags

    # 遍历所有配置的折扣规则
    @discounts.each do |discount|
      # 检查客户标签是否匹配
      customer_selector = CustomerTagSelector.new(discount[:customer_tag_match_type], discount[:customer_tags])
      next unless customer_selector.match?(cart.customer)

      # 处理产品标签匹配(如果配置了的话)
      product_selector = if discount[:product_tags]
        ProductTagSelector.new(:include, discount[:product_tags])
      else
        nil
      end

      cart.line_items.each do |line_item|
        product = line_item.variant.product
        # 如果有产品标签要求,先检查产品是否匹配
        next if product_selector && !product_selector.match?(product)

        # 应用对应折扣
        applicator = DiscountApplicator.new(discount[:discount_type], discount[:discount_amount], discount[:discount_message])
        applicator.apply(line_item)
      end
    end
  end
end

CAMPAIGNS = [
  DiscountForCustomerTagCampaign.new(DISCOUNTS_FOR_CUSTOMER_TAG),
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart)
end

Output.cart = Input.cart

关键修复说明

  1. 添加折扣规则遍历:在run方法中加入@discounts.each do |discount|循环,确保每个配置的折扣规则都被处理。
  2. 统一标签匹配逻辑:使用已有的ProductTagSelector类处理产品标签匹配,确保和客户标签一样做downcase.strip标准化,避免大小写/空白导致的匹配失败。
  3. 移除硬编码逻辑:删除冗余的硬编码折扣判断,直接通过配置数组中的规则应用折扣,提升代码维护性。
  4. 优化产品标签检查:仅当折扣规则配置了product_tags时,才进行产品标签匹配,兼容无产品标签限制的折扣规则。

内容的提问来源于stack exchange,提问作者Berzins.Kristaps

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 17:17:59