Migrating a capped percentage discount script
Common on high-value carts and the one place where getting it wrong is wrong in both directions at once: read the cap as the discount and you give $50 flat, drop the cap and you give an uncapped quarter of the cart.
可重建It worked out a percentage of the cart and then refused to take off more than a stated maximum.
把它粘贴到 Scripts 救援,看看会重建成什么
- 用本页任意 Script 旁边的按钮复制 Ruby 代码。
- 在 Shopify 后台打开 Stackable,依次进入工具、Scripts 救援。
- 粘贴进去,读一读工具说它会创建什么,再和本页的设置逐项对照。
Scripts 救援属于 Pro 套餐。在您填写旧 Script 的折扣金额、且两个数字精确到最小货币单位完全一致之前,不会发布任何内容。
Script 本体
可重建危险配对 D10。这个文件和它的搭档看起来几乎一样,行为却不同,在判断您手上是哪一个之前,请把两份都读一遍。
DISCOUNT_PERCENTAGE = 25
MAX_DISCOUNT = 50
cart_subtotal_float = Float((Input.cart.subtotal_price.cents / 100).to_s)
raw_discount = cart_subtotal_float * (DISCOUNT_PERCENTAGE / 100.0)
total_discount = [raw_discount, MAX_DISCOUNT].min
Input.cart.line_items.each do |line_item|
line_price_float = Float((line_item.line_price.cents / 100).to_s)
percentage = 1 - (((line_price_float / cart_subtotal_float) * total_discount) / line_price_float)
line_item.change_line_price(line_item.line_price * percentage, message: "25% off, up to $50")
end
Output.cart = Input.cart识别为 Spend threshold discount,重建为 spend_goal 类型的优惠。下面是优惠实际保存时使用的配置键,您可以逐项核对自己的重建结果。
| 设置项 | 值 | 配置键 |
|---|---|---|
| Offer type | spend_goal | type |
| Counting | per_cart | counting |
| Recurrence | once | recurrence |
| Tier 1 | min spend $0.00 -> 25% off | tiers[0] |
| Target scope | all | target.scope |
| Maximum discount | $50.00 | maxDiscountAmount |
| Method | automatic | discountMethod |
| Allocation | standard | stacking.allocation |
| Combines with other discounts | no | stacking |
- Confirm which products this applies to. We could not read product targeting from the script.
- The maximum applies to the whole order and is shared across the discounted items. If your Script capped each line on its own, that gave more away than this does.
基于我们的公开演示店铺 Meridian Goods 搭建,因此这里的每个价格都可以复现,而不必凭信任接受。下方的折扣金额,是结账时运行的同一个引擎针对这个购物车算出的结果。
| 商品 | 数量 | 单价 |
|---|---|---|
| Canvas Belt Bag | 1 | $42.00 |
| Aria Wireless Earbuds | 2 | $79.00 |
| 小计 | $200.00 | |
| 折扣 | -$50.00 | |
| 总额 | $150.00 | |
未登录,或以没有标签的客户身份登录。
发布之前,工具会询问您原来的 Script 对一个现有购物车做了什么。把它设为 8 件 Walnut Desk Organizer,单价 $26.00,答案就是固定的。
折扣金额为 $50.00。顾客实际支付的总额为 $158.00。
比对精确到您所用货币的最小单位。差一分钱,发布就仍然被拦住,这正是这一步的意义。
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 写法的两个版本。它们只差一两行,效果却不同,所以在判断自己手上是哪一个之前,请把两个都读一遍。
危险配对 D10
Capped percentage discount (D10)
可重建重建为 spend_goal 优惠。
在它自己的购物车中的折扣:$50.00
DISCOUNT_PERCENTAGE = 25
MAX_DISCOUNT = 50
cart_subtotal_float = Float((Input.cart.subtotal_price.cents / 100).to_s)
raw_discount = cart_subtotal_float * (DISCOUNT_PERCENTAGE / 100.0)
total_discount = [raw_discount, MAX_DISCOUNT].min
line_price_float = Float((line_item.line_price.cents / 100).to_s)另有 2 行差异,在它自己的页面上。
Fixed total discount cart (D10)
可重建重建为 spend_goal 优惠。
在它自己的购物车中的折扣:$14.00
MAX_DISCOUNT = Money.new(cents: 10000)
remaining = MAX_DISCOUNT
break if remaining <= Money.zero
share = [line_item.line_price, remaining].min
line_item.change_line_price(line_item.line_price - share, message: "$100 off your order")
remaining = remaining - share常见问题
- Will Stackable rebuild this script?
- Yes. Every spelling on this page is rebuilt today as a spend_goal offer, and the cart above is the one that proves it.
- What cart proves it works?
- 1 x Canvas Belt Bag at $42.00 plus 2 x Aria Wireless Earbuds at $79.00. The engine takes $50.00 off, and checkout charges $150.00.
- What will I have to check before publishing?
- Confirm which products this applies to. We could not read product targeting from the script. The maximum applies to the whole order and is shared across the discounted items. If your Script capped each line on its own, that gave more away than this does.
相关的 Script 类型
One lump sum off the whole cart, spread across the lines.
Everything in the clearance line repriced to one round number per unit.
A flat sum taken off every unit, however many the shopper bought.