跳转到主要内容
Script 示例

Migrating a script scoped to a tag, vendor or product type

A dropped filter is the most expensive class of mistake in a migration, because it is silent and it is confident: the percentage is read perfectly and applied to everything you sell. Note that Scripts could not read collections at all.

可重建
这个 Script 做了什么

It skipped every line whose product did not carry the listed tag, vendor or type.

商家用 3 种不同写法写过这段逻辑,它们的含义并不相同。每种写法都列在下方,各自配有对应的购物车和数字。

在您自己的店铺里试一试

把它粘贴到 Scripts 救援,看看会重建成什么

  1. 用本页任意 Script 旁边的按钮复制 Ruby 代码。
  2. 在 Shopify 后台打开 Stackable,依次进入工具、Scripts 救援。
  3. 粘贴进去,读一读工具说它会创建什么,再和本页的设置逐项对照。

Scripts 救援属于 Pro 套餐。在您填写旧 Script 的折扣金额、且两个数字精确到最小货币单位完全一致之前,不会发布任何内容。

Official dialect, product selector include (D13)

可重建

危险配对 D13。这个文件和它的搭档看起来几乎一样,行为却不同,在判断您手上是哪一个之前,请把两份都读一遍。

原始 Ruby

# ================================ Customizable Settings ================================
PRODUCT_DISCOUNTS = [
  {
    product_selector_match_type: :include,
    product_selector_type: :tag,
    product_selectors: ["sale"],
    discount_type: :percent,
    discount_amount: 20,
    discount_message: "20% off sale items",
  },
]

# ================================ Script Code (do not edit) ================================
class ProductSelector
  def initialize(match_type, selector_type, selectors)
    @match_type = match_type
    @comparator = match_type == :include ? 'any?' : 'none?'
    @selector_type = selector_type
    @selectors = selectors
  end

  def match?(line_item)
    if self.respond_to?(@selector_type)
      self.send(@selector_type, line_item)
    else
      raise RuntimeError.new('Invalid product selector type')
    end
  end

  def tag(line_item)
    product_tags = line_item.variant.product.tags.map { |tag| tag.downcase.strip }
    @selectors = @selectors.map { |selector| selector.downcase.strip }
    (@selectors & product_tags).send(@comparator)
  end

  def type(line_item)
    @selectors = @selectors.map { |selector| selector.downcase.strip }
    (@selectors.include?(line_item.variant.product.product_type.downcase.strip)) == (@match_type == :include)
  end

  def vendor(line_item)
    @selectors = @selectors.map { |selector| selector.downcase.strip }
    (@selectors.include?(line_item.variant.product.vendor.downcase.strip)) == (@match_type == :include)
  end

  def product_id(line_item)
    (@selectors.include?(line_item.variant.product.id)) == (@match_type == :include)
  end

  def variant_id(line_item)
    (@selectors.include?(line_item.variant.id)) == (@match_type == :include)
  end

  def all(line_item)
    true
  end
end

class ProductDiscountCampaign
  def initialize(campaigns)
    @campaigns = campaigns
  end

  def run(cart)
    @campaigns.each do |campaign|
      product_selector = ProductSelector.new(
        campaign[:product_selector_match_type],
        campaign[:product_selector_type],
        campaign[:product_selectors],
      )

      cart.line_items.each do |line_item|
        next unless product_selector.match?(line_item)
        line_item.change_line_price(
          line_item.line_price * (1 - (campaign[:discount_amount] * 0.01)),
          message: campaign[:discount_message],
        )
      end
    end
  end
end

CAMPAIGNS = [
  ProductDiscountCampaign.new(PRODUCT_DISCOUNTS),
]

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

Output.cart = Input.cart
它会变成什么

识别为 Percentage off,重建为 volume 类型的优惠。下面是优惠实际保存时使用的配置键,您可以逐项核对自己的重建结果。

逐项列出的设置
设置项配置键
Offer typevolumetype
Countingper_productcounting
Recurrenceoncerecurrence
Tier 1min qty 1 -> 20% offtiers[0]
Target scopetagstarget.scope
Target includessaletarget.includeIds
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
系统会请您确认
  • Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
用于验证的购物车

基于我们的公开演示店铺 Meridian Goods 搭建,因此这里的每个价格都可以复现,而不必凭信任接受。下方的折扣金额,是结账时运行的同一个引擎针对这个购物车算出的结果。

触发这个优惠的购物车,以及它的花费
商品数量单价
Ceramic Travel Mug1$22.00
小计$22.00
折扣-$4.40
总额$17.60

未登录,或以没有标签的客户身份登录。

工具用来核对您的金额

发布之前,工具会询问您原来的 Script 对一个现有购物车做了什么。把它设为 1 件 Ceramic Travel Mug,单价 $22.00,答案就是固定的。

折扣金额为 $4.40。顾客实际支付的总额为 $17.60。

比对精确到您所用货币的最小单位。差一分钱,发布就仍然被拦住,这正是这一步的意义。

必须没有任何结果的购物车
  • swap the cart for a single Braided USB-C Cable
不应触发这个优惠的购物车
商品数量单价
Braided USB-C Cable1$14.00
小计$14.00
总额$14.00

未登录,或以没有标签的客户身份登录。

Official dialect, product selector exclude (D13)

可重建

危险配对 D13。这个文件和它的搭档看起来几乎一样,行为却不同,在判断您手上是哪一个之前,请把两份都读一遍。

原始 Ruby

# ================================ Customizable Settings ================================
PRODUCT_DISCOUNTS = [
  {
    product_selector_match_type: :exclude,
    product_selector_type: :tag,
    product_selectors: ["sale"],
    discount_type: :percent,
    discount_amount: 20,
    discount_message: "20% off full-price items",
  },
]

# ================================ Script Code (do not edit) ================================
class ProductSelector
  def initialize(match_type, selector_type, selectors)
    @match_type = match_type
    @comparator = match_type == :include ? 'any?' : 'none?'
    @selector_type = selector_type
    @selectors = selectors
  end

  def match?(line_item)
    if self.respond_to?(@selector_type)
      self.send(@selector_type, line_item)
    else
      raise RuntimeError.new('Invalid product selector type')
    end
  end

  def tag(line_item)
    product_tags = line_item.variant.product.tags.map { |tag| tag.downcase.strip }
    @selectors = @selectors.map { |selector| selector.downcase.strip }
    (@selectors & product_tags).send(@comparator)
  end

  def all(line_item)
    true
  end
end

class ProductDiscountCampaign
  def initialize(campaigns)
    @campaigns = campaigns
  end

  def run(cart)
    @campaigns.each do |campaign|
      product_selector = ProductSelector.new(
        campaign[:product_selector_match_type],
        campaign[:product_selector_type],
        campaign[:product_selectors],
      )

      cart.line_items.each do |line_item|
        next unless product_selector.match?(line_item)
        line_item.change_line_price(
          line_item.line_price * (1 - (campaign[:discount_amount] * 0.01)),
          message: campaign[:discount_message],
        )
      end
    end
  end
end

CAMPAIGNS = [
  ProductDiscountCampaign.new(PRODUCT_DISCOUNTS),
]

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

Output.cart = Input.cart
它会变成什么

识别为 Percentage off,重建为 volume 类型的优惠。下面是优惠实际保存时使用的配置键,您可以逐项核对自己的重建结果。

逐项列出的设置
设置项配置键
Offer typevolumetype
Countingper_productcounting
Recurrenceoncerecurrence
Tier 1min qty 1 -> 20% offtiers[0]
Target scopealltarget.scope
Target excludessaletarget.excludeIds
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
系统会请您确认
  • Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
用于验证的购物车

基于我们的公开演示店铺 Meridian Goods 搭建,因此这里的每个价格都可以复现,而不必凭信任接受。下方的折扣金额,是结账时运行的同一个引擎针对这个购物车算出的结果。

  • Targeting excludes sale. No Meridian Goods product carries those, so every product stays eligible and the exclusion is proved by the campaign settings, not by the cart.
触发这个优惠的购物车,以及它的花费
商品数量单价
Braided USB-C Cable1$14.00
小计$14.00
折扣-$2.80
总额$11.20

未登录,或以没有标签的客户身份登录。

工具用来核对您的金额

发布之前,工具会询问您原来的 Script 对一个现有购物车做了什么。把它设为 1 件 Braided USB-C Cable,单价 $14.00,答案就是固定的。

折扣金额为 $2.80。顾客实际支付的总额为 $11.20。

比对精确到您所用货币的最小单位。差一分钱,发布就仍然被拦住,这正是这一步的意义。

必须没有任何结果的购物车
  • swap the cart for a single Ceramic Travel Mug
不应触发这个优惠的购物车
商品数量单价
Ceramic Travel Mug1$22.00
小计$22.00
总额$22.00

未登录,或以没有标签的客户身份登录。

Scope targeting family tag (D9)

可重建

危险配对 D9。这个文件和它的搭档看起来几乎一样,行为却不同,在判断您手上是哪一个之前,请把两份都读一遍。

原始 Ruby

Input.cart.line_items.each do |li|
  next unless li.variant.product.tags.include?('sale')
  li.change_line_price(li.original_line_price * (1.0 - (20 / 100.0)), message: "20% off sale")
end

Output.cart = Input.cart
它会变成什么

识别为 Percentage off,重建为 volume 类型的优惠。下面是优惠实际保存时使用的配置键,您可以逐项核对自己的重建结果。

逐项列出的设置
设置项配置键
Offer typevolumetype
Countingper_productcounting
Recurrenceoncerecurrence
Tier 1min qty 1 -> 20% offtiers[0]
Target scopetagstarget.scope
Target includessaletarget.includeIds
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
系统会请您确认
  • Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
用于验证的购物车

基于我们的公开演示店铺 Meridian Goods 搭建,因此这里的每个价格都可以复现,而不必凭信任接受。下方的折扣金额,是结账时运行的同一个引擎针对这个购物车算出的结果。

触发这个优惠的购物车,以及它的花费
商品数量单价
Ceramic Travel Mug1$22.00
小计$22.00
折扣-$4.40
总额$17.60

未登录,或以没有标签的客户身份登录。

工具用来核对您的金额

发布之前,工具会询问您原来的 Script 对一个现有购物车做了什么。把它设为 1 件 Ceramic Travel Mug,单价 $22.00,答案就是固定的。

折扣金额为 $4.40。顾客实际支付的总额为 $17.60。

比对精确到您所用货币的最小单位。差一分钱,发布就仍然被拦住,这正是这一步的意义。

必须没有任何结果的购物车
  • swap the cart for a single Braided USB-C Cable
不应触发这个优惠的购物车
商品数量单价
Braided USB-C Cable1$14.00
小计$14.00
总额$14.00

未登录,或以没有标签的客户身份登录。

两个看起来一样的文件

下面每一对都是同一种 Script 写法的两个版本。它们只差一两行,效果却不同,所以在判断自己手上是哪一个之前,请把两个都读一遍。

危险配对 D9

Scope targeting family tag (D9)

可重建

重建为 volume 优惠。

在它自己的购物车中的折扣:$4.40

不同的行
  next unless li.variant.product.tags.include?('sale')
  li.change_line_price(li.original_line_price * (1.0 - (20 / 100.0)), message: "20% off sale")
阅读Scoped to a tag or vendor

Flat percentage off all (D9)

可重建

重建为 volume 优惠。

在它自己的购物车中的折扣:$2.80

不同的行
  li.change_line_price(li.original_line_price * (1.0 - (20 / 100.0)), message: "20% off everything")
阅读Sitewide percentage off

危险配对 D13

Official dialect, product selector include (D13)

可重建

重建为 volume 优惠。

在它自己的购物车中的折扣:$4.40

不同的行
    product_selector_match_type: :include,
    discount_message: "20% off sale items",
  def type(line_item)
    @selectors = @selectors.map { |selector| selector.downcase.strip }
    (@selectors.include?(line_item.variant.product.product_type.downcase.strip)) == (@match_type == :include)
  end

另有 10 行差异,在它自己的页面上。

阅读Scoped to a tag or vendor

Official dialect, product selector exclude (D13)

可重建

重建为 volume 优惠。

在它自己的购物车中的折扣:$2.80

不同的行
    product_selector_match_type: :exclude,
    discount_message: "20% off full-price items",
阅读Scoped to a tag or vendor

常见问题

Will Stackable rebuild this script?
Yes. Every spelling on this page is rebuilt today as a volume offer, and the cart above is the one that proves it.
What cart proves it works?
1 x Ceramic Travel Mug at $22.00. The engine takes $4.40 off, and checkout charges $17.60.
What will I have to check before publishing?
Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.

相关的 Script 类型

Sitewide percentage off

One percentage off the whole catalogue, with no condition attached.

Exclude sale items

Already-marked-down products protected from a second discount.

Exclude gift cards

Gift cards left out of a discount that would otherwise have hit them.

粘贴您的 Script,看看它如何被重建

在您发布之前,重建结果会与原来的 Script 收取的金额进行核对。

我们使用必要 Cookie 来运行本网站,并仅在获得你许可的情况下,使用分析 Cookie 来了解访问情况。详见我们的 Cookie 政策.