Migrating a Shopify Script Creator script
Merchants who could not write Ruby used a generator, so the most common paste in the world is not hand-written at all. The 200 lines of class definitions are boilerplate; the array at the bottom is the offer.
可重建It ran each campaign in the CAMPAIGNS array against the cart, in order, using a fixed library of qualifier and discount classes.
把它粘贴到 Scripts 救援,看看会重建成什么
- 用本页任意 Script 旁边的按钮复制 Ruby 代码。
- 在 Shopify 后台打开 Stackable,依次进入工具、Scripts 救援。
- 粘贴进去,读一读工具说它会创建什么,再和本页的设置逐项对照。
Scripts 救援属于 Pro 套餐。在您填写旧 Script 的折扣金额、且两个数字精确到最小货币单位完全一致之前,不会发布任何内容。
Script 本体
可重建# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
# Version 0.12.0
class Campaign
def initialize(condition, *qualifiers)
@condition = (condition.to_s + '?').to_sym
@qualifiers = PostCartAmountQualifier ? [] : [] rescue qualifiers.compact
@line_item_selector = qualifiers.last unless @line_item_selector
qualifiers.compact.each do |qualifier|
is_multi_select = qualifier.instance_variable_get(:@conditions).is_a?(Array)
if is_multi_select
qualifier.instance_variable_get(:@conditions).each do |nested_q|
@post_amount_qualifier = nested_q if nested_q.is_a?(PostCartAmountQualifier)
@qualifiers << qualifier
end
else
@post_amount_qualifier = qualifier if qualifier.is_a?(PostCartAmountQualifier)
@qualifiers << qualifier
end
end if @qualifiers.empty?
end
def qualifies?(cart)
return true if @qualifiers.empty?
@qualifiers.send(@condition) do |qualifier|
is_selector = false
if qualifier.is_a?(Selector) || qualifier.is_a?(Array)
is_selector = true
end rescue nil
if is_selector
raise 'Missing line item match type' if @li_match_type.nil?
cart.line_items.send(@li_match_type) { |item| qualifier.match?(item) }
else
qualifier.match?(cart, @selector)
end
end
end
end
class ProductTagSelector < Selector
def initialize(match_type, match_condition, tags)
@match_condition = match_condition
@invert = match_type == :does_not
@tags = tags.map(&:downcase)
end
def match?(line_item)
product_tags = line_item.variant.product.tags.to_a.map(&:downcase)
case @match_condition
when :match
return @invert ^ ((@tags & product_tags).length > 0)
else
return @invert ^ partial_match(@match_condition, product_tags, @tags)
end
end
end
class PercentageDiscount
def initialize(percent, message)
@percent = Decimal.new(percent) / 100.0
@message = message
end
def apply(line_item)
line_discount = line_item.line_price * @percent
new_line_price = line_item.line_price - line_discount
line_item.change_line_price(new_line_price, message: @message)
end
end
CAMPAIGNS = [
ConditionalDiscount.new(
:all,
nil,
nil,
ProductTagSelector.new(:does, :match, ["sale"]),
PercentageDiscount.new(20, "20% off sale items"),
0
),
].freeze
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cart识别为 Percentage off,重建为 volume 类型的优惠。下面是优惠实际保存时使用的配置键,您可以逐项核对自己的重建结果。
| 设置项 | 值 | 配置键 |
|---|---|---|
| Offer type | volume | type |
| Counting | per_product | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 1 -> 20% off | tiers[0] |
| Target scope | tags | target.scope |
| Target includes | sale | target.includeIds |
| Method | automatic | discountMethod |
| Allocation | standard | stacking.allocation |
| Combines with other discounts | no | stacking |
- 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 Mug | 1 | $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 Cable | 1 | $14.00 |
| 小计 | $14.00 | |
| 总额 | $14.00 | |
未登录,或以没有标签的客户身份登录。
常见问题
- 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 类型
The dialect Shopify’s own documentation taught, with its settings block above a do-not-edit banner.