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.
再構築可It skipped every line whose product did not carry the listed tag, vendor or type.
この処理はマーチャントによって 3 通りの書き方がされており、すべてが同じ意味とは限りません。それぞれの書き方を、専用のカートと数値とともに以下に示します。
スクリプトレスキューに貼り付けて、何が再現されるか確認する
- このページの各スクリプトの横にあるボタンで Ruby をコピーします。
- Shopify 管理画面で Stackable を開き、ツール、スクリプトレスキューの順に進みます。
- そこに貼り付け、ツールが作成すると示した内容を読み、このページの設定と見比べます。
スクリプトレスキューは Pro プランの機能です。以前のスクリプトの割引額を入力し、両方の金額が最小通貨単位まで一致するまで、何も公開されません。
Official dialect, product selector include (D13)
再構築可危険なペア D13。このファイルと対になるファイルは見た目がほぼ同じで、動作が異なります。どちらを使っているか判断する前に両方を読んでください。
# ================================ 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.cartPercentage 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 が何をしていたかを尋ねます。カートを Ceramic Travel Mug 1、単価 $22.00 に設定すれば、答えは一つに定まります。
割引額は $4.40 です。購入者が支払った合計は $17.60 です。
照合は通貨の最小単位まで完全一致で行われます。1 単位でもずれれば公開はできません。それがこの手順の目的です。
- swap the cart for a single Braided USB-C Cable
| 商品 | 数量 | 単価 |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| 小計 | $14.00 | |
| 合計 | $14.00 | |
未ログイン、またはタグのない顧客としてログインしています。
Official dialect, product selector exclude (D13)
再構築可危険なペア D13。このファイルと対になるファイルは見た目がほぼ同じで、動作が異なります。どちらを使っているか判断する前に両方を読んでください。
# ================================ 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.cartPercentage off として認識され、volume タイプのオファーとして再構築されます。以下はオファーが実際に保存される設定キーそのものなので、ご自身の再構築結果を項目ごとに照合できます。
| 設定項目 | 値 | 設定キー |
|---|---|---|
| Offer type | volume | type |
| Counting | per_product | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 1 -> 20% off | tiers[0] |
| Target scope | all | target.scope |
| Target excludes | sale | target.excludeIds |
| 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 で作成しているため、ここに載せた価格はすべて、信じるのではなく再現して確かめられます。以下の割引額は、決済時に動くのと同じエンジンがこのカートに対して算出した金額です。
- 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 Cable | 1 | $14.00 |
| 小計 | $14.00 | |
| 割引 | -$2.80 | |
| 合計 | $11.20 | |
未ログイン、またはタグのない顧客としてログインしています。
公開前に、ツールは手元のカートに対して以前の Script が何をしていたかを尋ねます。カートを Braided USB-C Cable 1、単価 $14.00 に設定すれば、答えは一つに定まります。
割引額は $2.80 です。購入者が支払った合計は $11.20 です。
照合は通貨の最小単位まで完全一致で行われます。1 単位でもずれれば公開はできません。それがこの手順の目的です。
- swap the cart for a single Ceramic Travel Mug
| 商品 | 数量 | 単価 |
|---|---|---|
| Ceramic Travel Mug | 1 | $22.00 |
| 小計 | $22.00 | |
| 合計 | $22.00 | |
未ログイン、またはタグのない顧客としてログインしています。
Scope targeting family tag (D9)
再構築可危険なペア D9。このファイルと対になるファイルは見た目がほぼ同じで、動作が異なります。どちらを使っているか判断する前に両方を読んでください。
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.cartPercentage 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 が何をしていたかを尋ねます。カートを Ceramic Travel Mug 1、単価 $22.00 に設定すれば、答えは一つに定まります。
割引額は $4.40 です。購入者が支払った合計は $17.60 です。
照合は通貨の最小単位まで完全一致で行われます。1 単位でもずれれば公開はできません。それがこの手順の目的です。
- swap the cart for a single Braided USB-C Cable
| 商品 | 数量 | 単価 |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| 小計 | $14.00 | |
| 合計 | $14.00 | |
未ログイン、またはタグのない顧客としてログインしています。
見た目がそっくりな 2 つのファイル
以下の各ペアは、同じ形のスクリプトを 2 通りに書いたものです。違いは 1 行か 2 行ですが、動作は異なります。どちらが手元のものか決める前に、両方を読んでください。
紛らわしいペア 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")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")紛らわしいペア 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 行の違いが、それぞれのページにあります。
Official dialect, product selector exclude (D13)
再構築可volume オファーとして再現されます。
このカートでの割引: $2.80
product_selector_match_type: :exclude,
discount_message: "20% off full-price items",よくある質問
- 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 の型
One percentage off the whole catalogue, with no condition attached.
Already-marked-down products protected from a second discount.
Gift cards left out of a discount that would otherwise have hit them.