跳转到主要内容
Script 示例

Migrating a percentage off shipping script

The shipping half of Shopify’s own dialect uses the same class name as the line-item half and stores the opposite fraction in it. One keeps what the shopper pays, the other keeps what comes off.

可重建
这个 Script 做了什么

It reduced every delivery rate at checkout by a percentage.

在您自己的店铺里试一试

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

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

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

Script 本体

可重建

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

原始 Ruby

# ================================ Customizable Settings ================================
DISCOUNT_TYPE = :percent
DISCOUNT_AMOUNT = 10
DISCOUNT_MESSAGE = '10% off shipping'

# ================================ Script Code (do not edit) ================================
class DiscountApplicator
  def initialize(discount_type, discount_amount, discount_message)
    @discount_type = discount_type
    @discount_message = discount_message

    @discount_amount = if discount_type == :percent
      discount_amount * 0.01
    else
      Money.new(cents: 100) * discount_amount
    end
  end

  def apply(shipping_rate)
    discount_amount = if @discount_type == :percent
      shipping_rate.price * @discount_amount
    else
      @discount_amount > shipping_rate.price ? shipping_rate.price : @discount_amount
    end

    shipping_rate.apply_discount(discount_amount, message: @discount_message)
  end
end

class DiscountRatesCampaign
  def initialize(discount_applicator)
    @discount_applicator = discount_applicator
  end

  def run(cart, shipping_rates)
    shipping_rates.each do |shipping_rate|
      @discount_applicator.apply(shipping_rate)
    end
  end
end

CAMPAIGNS = [
  DiscountRatesCampaign.new(
    DiscountApplicator.new(DISCOUNT_TYPE, DISCOUNT_AMOUNT, DISCOUNT_MESSAGE),
  ),
]

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

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

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

逐项列出的设置
设置项配置键
Offer typefree_shippingtype
Countingper_cartcounting
Recurrenceoncerecurrence
Tier 1min spend $0.00 -> 10% offtiers[0]
Target scopealltarget.scope
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
系统会请您确认
  • Confirm which products this applies to. We could not read product targeting from the script.
系统会告知您
  • This discounts every shipping rate at checkout, not one named rate.
用于验证的购物车

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

触发这个优惠的购物车,以及它的花费
商品数量单价
Braided USB-C Cable1$14.00
小计$14.00
配送,通常为 $6.90$6.21
总额$14.00

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

工具用来核对您的金额

This rebuild only changes shipping. We price shipping against a sample rate rather than the rate your shopper saw, so there is no figure here we could honestly compare. Publishing records that the amount was not checked.

必须没有任何结果的购物车

This rule carries no condition at all, so it fires on any cart holding anything and there is no quiet cart to build. The negative check is an empty cart, which the app runs for you. That is correct behaviour, not a failure.

两个看起来一样的文件

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

危险配对 D16

Official dialect, shipping applicator (D16)

可重建

重建为 free_shipping 优惠。

在它自己的购物车中的运费节省:$0.69

不同的行
DISCOUNT_MESSAGE = '10% off shipping'
      discount_amount * 0.01
  def apply(shipping_rate)
    discount_amount = if @discount_type == :percent
      shipping_rate.price * @discount_amount
      @discount_amount > shipping_rate.price ? shipping_rate.price : @discount_amount

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

阅读Percentage off shipping

Official dialect, lineitem applicator (D16)

可重建

重建为 volume 优惠。

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

不同的行
DISCOUNT_MESSAGE = '10% off everything'
      1 - (discount_amount * 0.01)
  def apply(line_item)
    new_line_price = if @discount_type == :percent
      line_item.line_price * @discount_amount
      [line_item.line_price - (@discount_amount * line_item.quantity), Money.zero].max

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

阅读Help Center script template

常见问题

Will Stackable rebuild this script?
Yes. Every spelling on this page is rebuilt today as a free_shipping offer, and the cart above is the one that proves it.
What cart proves it works?
1 x Braided USB-C Cable at $14.00. The engine takes $0.69 off, and checkout charges $14.00 plus $6.21 delivery.
What will I have to check before publishing?
Confirm which products this applies to. We could not read product targeting from the script. This discounts every shipping rate at checkout, not one named rate.

相关的 Script 类型

Help Center script template

The dialect Shopify’s own documentation taught, with its settings block above a do-not-edit banner.

Amount off shipping

A flat sum off the delivery charge, floored at free.

Flat rate shipping

Every delivery rate rewritten to one price rather than reduced by an amount.

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

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

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