Migrating a script that took an amount off each item
Written four different ways in the wild, and two of them differ only in whether the number in the file already meant cents. Reading a generator’s "5" as five cents is a hundredfold error, so all four spellings are shown side by side.
再構築可It subtracted a fixed sum from every matching unit in the cart.
この処理はマーチャントによって 4 通りの書き方がされており、すべてが同じ意味とは限りません。それぞれの書き方を、専用のカートと数値とともに以下に示します。
スクリプトレスキューに貼り付けて、何が再現されるか確認する
- このページの各スクリプトの横にあるボタンで Ruby をコピーします。
- Shopify 管理画面で Stackable を開き、ツール、スクリプトレスキューの順に進みます。
- そこに貼り付け、ツールが作成すると示した内容を読み、このページの設定と見比べます。
スクリプトレスキューは Pro プランの機能です。以前のスクリプトの割引額を入力し、両方の金額が最小通貨単位まで一致するまで、何も公開されません。
Amount off per unit (D1)
再構築可危険なペア D1。このファイルと対になるファイルは見た目がほぼ同じで、動作が異なります。どちらを使っているか判断する前に両方を読んでください。
DISCOUNT = Money.new(cents: 999)
Input.cart.line_items.each do |line_item|
next unless line_item.variant.product.product_type == "Clearance"
new_price = line_item.line_price - (DISCOUNT * line_item.quantity)
line_item.change_line_price(new_price, message: "$9.99 off each")
end
Output.cart = Input.cartAmount off at a quantity として認識され、volume タイプのオファーとして再構築されます。以下はオファーが実際に保存される設定キーそのものなので、ご自身の再構築結果を項目ごとに照合できます。
| 設定項目 | 値 | 設定キー |
|---|---|---|
| Offer type | volume | type |
| Counting | per_product | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 1 -> $9.99 off each | tiers[0] |
| Target scope | product_types | target.scope |
| Target includes | Clearance | 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 で作成しているため、ここに載せた価格はすべて、信じるのではなく再現して確かめられます。以下の割引額は、決済時に動くのと同じエンジンがこのカートに対して算出した金額です。
- No Meridian Goods product carries the type Clearance. Set it on Aria Wireless Earbuds in Shopify before you build the cart, and on nothing else.
| 商品 | 数量 | 単価 |
|---|---|---|
| Aria Wireless Earbuds | 1 | $79.00 |
| 小計 | $79.00 | |
| 割引 | -$9.99 | |
| 合計 | $69.01 | |
未ログイン、またはタグのない顧客としてログインしています。
公開前に、ツールは手元のカートに対して以前の Script が何をしていたかを尋ねます。カートを Aria Wireless Earbuds 1、単価 $79.00 に設定すれば、答えは一つに定まります。
割引額は $9.99 です。購入者が支払った合計は $69.01 です。
照合は通貨の最小単位まで完全一致で行われます。1 単位でもずれれば公開はできません。それがこの手順の目的です。
- swap the cart for a single Braided USB-C Cable
| 商品 | 数量 | 単価 |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| 小計 | $14.00 | |
| 合計 | $14.00 | |
未ログイン、またはタグのない顧客としてログインしています。
Amount off per unit (D7)
再構築可危険なペア D7。このファイルと対になるファイルは見た目がほぼ同じで、動作が異なります。どちらを使っているか判断する前に両方を読んでください。
# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
class FixedItemDiscount
def initialize(amount, message)
@amount = Money.new(cents: 100) * amount
@message = message
end
def apply(line_item)
per_item_discount = @amount
discount_to_apply = [(per_item_discount * line_item.quantity), line_item.line_price].min
line_item.change_line_price([line_item.line_price - discount_to_apply, Money.zero].max, message: @message)
end
end
CAMPAIGNS = [
ConditionalDiscount.new(:all, nil, nil, nil, FixedItemDiscount.new(5, "$5 off each"), 0),
].freeze
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cartAmount off at a quantity として認識され、volume タイプのオファーとして再構築されます。以下はオファーが実際に保存される設定キーそのものなので、ご自身の再構築結果を項目ごとに照合できます。
| 設定項目 | 値 | 設定キー |
|---|---|---|
| Offer type | volume | type |
| Counting | per_product | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 1 -> $5.00 off each | tiers[0] |
| Target scope | all | target.scope |
| 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.
公開デモストア Meridian Goods で作成しているため、ここに載せた価格はすべて、信じるのではなく再現して確かめられます。以下の割引額は、決済時に動くのと同じエンジンがこのカートに対して算出した金額です。
| 商品 | 数量 | 単価 |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| 小計 | $14.00 | |
| 割引 | -$5.00 | |
| 合計 | $9.00 | |
未ログイン、またはタグのない顧客としてログインしています。
公開前に、ツールは手元のカートに対して以前の Script が何をしていたかを尋ねます。カートを Braided USB-C Cable 1、単価 $14.00 に設定すれば、答えは一つに定まります。
割引額は $5.00 です。購入者が支払った合計は $9.00 です。
照合は通貨の最小単位まで完全一致で行われます。1 単位でもずれれば公開はできません。それがこの手順の目的です。
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.
Amount off per unit dollars (D11)
再構築可危険なペア D11。このファイルと対になるファイルは見た目がほぼ同じで、動作が異なります。どちらを使っているか判断する前に両方を読んでください。
# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
class FixedItemDiscount
def initialize(amount, message)
@amount = Money.new(cents: 100) * amount
@message = message
end
def apply(line_item)
discount_to_apply = [(@amount * line_item.quantity), line_item.line_price].min
line_item.change_line_price([line_item.line_price - discount_to_apply, Money.zero].max, message: @message)
end
end
CAMPAIGNS = [
ConditionalDiscount.new(:all, nil, nil, nil, FixedItemDiscount.new(5, "$5 off each"), 0),
].freeze
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cartAmount off at a quantity として認識され、volume タイプのオファーとして再構築されます。以下はオファーが実際に保存される設定キーそのものなので、ご自身の再構築結果を項目ごとに照合できます。
| 設定項目 | 値 | 設定キー |
|---|---|---|
| Offer type | volume | type |
| Counting | per_product | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 1 -> $5.00 off each | tiers[0] |
| Target scope | all | target.scope |
| 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.
公開デモストア Meridian Goods で作成しているため、ここに載せた価格はすべて、信じるのではなく再現して確かめられます。以下の割引額は、決済時に動くのと同じエンジンがこのカートに対して算出した金額です。
| 商品 | 数量 | 単価 |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| 小計 | $14.00 | |
| 割引 | -$5.00 | |
| 合計 | $9.00 | |
未ログイン、またはタグのない顧客としてログインしています。
公開前に、ツールは手元のカートに対して以前の Script が何をしていたかを尋ねます。カートを Braided USB-C Cable 1、単価 $14.00 に設定すれば、答えは一つに定まります。
割引額は $5.00 です。購入者が支払った合計は $9.00 です。
照合は通貨の最小単位まで完全一致で行われます。1 単位でもずれれば公開はできません。それがこの手順の目的です。
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.
Amount off per unit cents (D11)
再構築可危険なペア D11。このファイルと対になるファイルは見た目がほぼ同じで、動作が異なります。どちらを使っているか判断する前に両方を読んでください。
DISCOUNT = Money.new(cents: 500)
Input.cart.line_items.each do |line_item|
discount_to_apply = [(DISCOUNT * line_item.quantity), line_item.line_price].min
line_item.change_line_price([line_item.line_price - discount_to_apply, Money.zero].max, message: "$5 off each")
end
Output.cart = Input.cartAmount off at a quantity として認識され、volume タイプのオファーとして再構築されます。以下はオファーが実際に保存される設定キーそのものなので、ご自身の再構築結果を項目ごとに照合できます。
| 設定項目 | 値 | 設定キー |
|---|---|---|
| Offer type | volume | type |
| Counting | per_product | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 1 -> $5.00 off each | tiers[0] |
| Target scope | all | target.scope |
| 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.
公開デモストア Meridian Goods で作成しているため、ここに載せた価格はすべて、信じるのではなく再現して確かめられます。以下の割引額は、決済時に動くのと同じエンジンがこのカートに対して算出した金額です。
| 商品 | 数量 | 単価 |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| 小計 | $14.00 | |
| 割引 | -$5.00 | |
| 合計 | $9.00 | |
未ログイン、またはタグのない顧客としてログインしています。
公開前に、ツールは手元のカートに対して以前の Script が何をしていたかを尋ねます。カートを Braided USB-C Cable 1、単価 $14.00 に設定すれば、答えは一つに定まります。
割引額は $5.00 です。購入者が支払った合計は $9.00 です。
照合は通貨の最小単位まで完全一致で行われます。1 単位でもずれれば公開はできません。それがこの手順の目的です。
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.
見た目がそっくりな 2 つのファイル
以下の各ペアは、同じ形のスクリプトを 2 通りに書いたものです。違いは 1 行か 2 行ですが、動作は異なります。どちらが手元のものか決める前に、両方を読んでください。
紛らわしいペア D1
Amount off per unit (D1)
再構築可volume オファーとして再現されます。
このカートでの割引: $9.99
DISCOUNT = Money.new(cents: 999)
new_price = line_item.line_price - (DISCOUNT * line_item.quantity)
line_item.change_line_price(new_price, message: "$9.99 off each")Fixed price per unit (D1)
再構築可volume オファーとして再現されます。
このカートでの割引: $69.01
FIXED_PRICE = Money.new(cents: 999)
line_item.change_line_price(FIXED_PRICE * line_item.quantity, message: "Clearance $9.99 each")紛らわしいペア D7
Amount off per unit (D7)
再構築可volume オファーとして再現されます。
このカートでの割引: $5.00
class FixedItemDiscount
def initialize(amount, message)
per_item_discount = @amount
discount_to_apply = [(per_item_discount * line_item.quantity), line_item.line_price].min
line_item.change_line_price([line_item.line_price - discount_to_apply, Money.zero].max, message: @message)
ConditionalDiscount.new(:all, nil, nil, nil, FixedItemDiscount.new(5, "$5 off each"), 0),Fixed total discount cart (D7)
再構築可spend_goal オファーとして再現されます。
このカートでの割引: $5.00
class FixedTotalDiscount
def initialize(amount, message, behaviour = :split)
@discount_applied = Money.zero
@behaviour = behaviour
return unless @discount_applied < @amount
discount_to_apply = [(@amount - @discount_applied), line_item.line_price].minさらに 3 行の違いが、それぞれのページにあります。
紛らわしいペア D11
Amount off per unit dollars (D11)
再構築可volume オファーとして再現されます。
このカートでの割引: $5.00
# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
class FixedItemDiscount
def initialize(amount, message)
@amount = Money.new(cents: 100) * amount
@message = message
endさらに 10 行の違いが、それぞれのページにあります。
Amount off per unit cents (D11)
再構築可volume オファーとして再現されます。
このカートでの割引: $5.00
DISCOUNT = Money.new(cents: 500)
Input.cart.line_items.each do |line_item|
discount_to_apply = [(DISCOUNT * line_item.quantity), line_item.line_price].min
line_item.change_line_price([line_item.line_price - discount_to_apply, Money.zero].max, message: "$5 off each")よくある質問
- 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 Aria Wireless Earbuds at $79.00. The engine takes $9.99 off, and checkout charges $69.01.
- 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. Confirm which products this applies to. We could not read product targeting from the script.
関連する Script の型
Everything in the clearance line repriced to one round number per unit.
One lump sum off the whole cart, spread across the lines.
One percentage off the whole catalogue, with no condition attached.