コンテンツにスキップ
Script 実例集

Migrating a script that limited how many units were discounted

The cap is not the trap. The trap is a single sort line: with it the CHEAPEST units are discounted, without it the FIRST ones are, and both files are dense with the same split call that also makes them look like a BOGO.

再構築可
この Script がしていたこと

It discounted only the first few matching units and left the rest at full price.

この処理はマーチャントによって 3 通りの書き方がされており、すべてが同じ意味とは限りません。それぞれの書き方を、専用のカートと数値とともに以下に示します。

自分のストアで試す

スクリプトレスキューに貼り付けて、何が再現されるか確認する

  1. このページの各スクリプトの横にあるボタンで Ruby をコピーします。
  2. Shopify 管理画面で Stackable を開き、ツール、スクリプトレスキューの順に進みます。
  3. そこに貼り付け、ツールが作成すると示した内容を読み、このページの設定と見比べます。

スクリプトレスキューは Pro プランの機能です。以前のスクリプトの割引額を入力し、両方の金額が最小通貨単位まで一致するまで、何も公開されません。

Quantity limit first n discounted (D5)

再構築可

危険なペア D5。このファイルと対になるファイルは見た目がほぼ同じで、動作が異なります。どちらを使っているか判断する前に両方を読んでください。

元の Ruby

# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP

class ConditionalDiscount < Campaign
  def initialize(condition, customer_qualifier, cart_qualifier, line_item_selector, discount, max_discounts)
    super(condition, customer_qualifier, cart_qualifier, line_item_selector)
    @discount = discount
    @items_to_discount = max_discounts == 0 ? nil : max_discounts
  end

  def run(cart)
    return unless qualifies?(cart)
    applicable_items = cart.line_items.select { |item| @line_item_selector.nil? || @line_item_selector.match?(item) }
    applicable_items.each do |item|
      break if @items_to_discount == 0
      if !@items_to_discount.nil? && (item.quantity > @items_to_discount)
        new_item = item.split({ take: @items_to_discount })
        @discount.apply(new_item)
        position = cart.line_items.find_index(item)
        cart.line_items.insert(position + 1, new_item)
        @items_to_discount = 0
      else
        @discount.apply(item)
        @items_to_discount -= item.quantity unless @items_to_discount.nil?
      end
    end
  end
end

CAMPAIGNS = [
  ConditionalDiscount.new(
    :all, nil, nil,
    ProductTypeSelector.new(:does, ["Sale"]),
    PercentageDiscount.new(30, "30% off first 2"),
    2
  ),
].freeze

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

Output.cart = Input.cart
何になるか

Discount on the first few units として認識され、bxgy タイプのオファーとして再構築されます。以下はオファーが実際に保存される設定キーそのものなので、ご自身の再構築結果を項目ごとに照合できます。

すべての設定項目
設定項目設定キー
Offer typebxgytype
Countingper_variantcounting
Recurrenceoncerecurrence
Tier 1min qty 1 -> free itemtiers[0]
Buy quantity1bxgy.buyQty
Get quantity2bxgy.getQty
Get selectionsame_itembxgy.getSelection
Reward on the get units30% offbxgy.getDiscountPct
Free units are additionalfalsebxgy.freeUnitsAreAdditional
Target scopeproduct_typestarget.scope
Target includesSaletarget.includeIds
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
確認を求められます
  • Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
次の点が表示されます
  • The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
それを裏づけるカート

公開デモストア Meridian Goods で作成しているため、ここに載せた価格はすべて、信じるのではなく再現して確かめられます。以下の割引額は、決済時に動くのと同じエンジンがこのカートに対して算出した金額です。

  • No Meridian Goods product carries the type Sale. Set it on Aria Wireless Earbuds in Shopify before you build the cart, and on nothing else.
このオファーが発動するカートと、その金額
商品数量単価
Aria Wireless Earbuds1$79.00
小計$79.00
割引-$23.70
合計$55.30

未ログイン、またはタグのない顧客としてログインしています。

ツールが照合する金額

公開前に、ツールは手元のカートに対して以前の Script が何をしていたかを尋ねます。カートを Aria Wireless Earbuds 1、単価 $79.00 に設定すれば、答えは一つに定まります。

割引額は $23.70 です。購入者が支払った合計は $55.30 です。

照合は通貨の最小単位まで完全一致で行われます。1 単位でもずれれば公開はできません。それがこの手順の目的です。

何も起きてはいけないカート
  • swap the cart for a single Braided USB-C Cable
このオファーが発動してはいけないカート
商品数量単価
Braided USB-C Cable1$14.00
小計$14.00
合計$14.00

未ログイン、またはタグのない顧客としてログインしています。

Quantity limit cheapest n (D5)

再構築可

危険なペア D5。このファイルと対になるファイルは見た目がほぼ同じで、動作が異なります。どちらを使っているか判断する前に両方を読んでください。

元の Ruby

# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP

class ConditionalDiscount < Campaign
  def initialize(condition, customer_qualifier, cart_qualifier, line_item_selector, discount, max_discounts)
    super(condition, customer_qualifier, cart_qualifier, line_item_selector)
    @discount = discount
    @items_to_discount = max_discounts == 0 ? nil : max_discounts
  end

  def run(cart)
    return unless qualifies?(cart)
    applicable_items = cart.line_items.select { |item| @line_item_selector.nil? || @line_item_selector.match?(item) }
    applicable_items = applicable_items.sort_by { |item| item.variant.price }
    applicable_items.each do |item|
      break if @items_to_discount == 0
      if !@items_to_discount.nil? && (item.quantity > @items_to_discount)
        new_item = item.split({ take: @items_to_discount })
        @discount.apply(new_item)
        position = cart.line_items.find_index(item)
        cart.line_items.insert(position + 1, new_item)
        @items_to_discount = 0
      else
        @discount.apply(item)
        @items_to_discount -= item.quantity unless @items_to_discount.nil?
      end
    end
  end
end

CAMPAIGNS = [
  ConditionalDiscount.new(
    :all, nil, nil,
    ProductTypeSelector.new(:does, ["Sale"]),
    PercentageDiscount.new(30, "30% off your 2 cheapest"),
    2
  ),
].freeze

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

Output.cart = Input.cart
何になるか

Discount on the first few units として認識され、bxgy タイプのオファーとして再構築されます。以下はオファーが実際に保存される設定キーそのものなので、ご自身の再構築結果を項目ごとに照合できます。

すべての設定項目
設定項目設定キー
Offer typebxgytype
Countingper_groupcounting
Recurrenceoncerecurrence
Tier 1min qty 1 -> free itemtiers[0]
Buy quantity1bxgy.buyQty
Get quantity2bxgy.getQty
Get selectioncheapest_eligiblebxgy.getSelection
Reward on the get units30% offbxgy.getDiscountPct
Free units are additionalfalsebxgy.freeUnitsAreAdditional
Target scopeproduct_typestarget.scope
Target includesSaletarget.includeIds
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
確認を求められます
  • Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
次の点が表示されます
  • The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
それを裏づけるカート

公開デモストア Meridian Goods で作成しているため、ここに載せた価格はすべて、信じるのではなく再現して確かめられます。以下の割引額は、決済時に動くのと同じエンジンがこのカートに対して算出した金額です。

  • No Meridian Goods product carries the type Sale. Set it on Aria Wireless Earbuds and Impact Phone Case in Shopify before you build the cart, and on nothing else.
このオファーが発動するカートと、その金額
商品数量単価
Impact Phone Case1$24.00
Aria Wireless Earbuds1$79.00
小計$103.00
割引-$30.90
合計$72.10

未ログイン、またはタグのない顧客としてログインしています。

ツールが照合する金額

公開前に、ツールは手元のカートに対して以前の Script が何をしていたかを尋ねます。カートを Impact Phone Case 1、単価 $24.00 に設定すれば、答えは一つに定まります。

割引額は $7.20 です。購入者が支払った合計は $16.80 です。

照合は通貨の最小単位まで完全一致で行われます。1 単位でもずれれば公開はできません。それがこの手順の目的です。

何も起きてはいけないカート
  • swap the cart for a single Braided USB-C Cable
このオファーが発動してはいけないカート
商品数量単価
Braided USB-C Cable1$14.00
小計$14.00
合計$14.00

未ログイン、またはタグのない顧客としてログインしています。

Quantity limit first n discounted (D6)

再構築可

危険なペア D6。このファイルと対になるファイルは見た目がほぼ同じで、動作が異なります。どちらを使っているか判断する前に両方を読んでください。

元の Ruby

DISCOUNT_PERCENTAGE = 30
MAX_UNITS = 2

num_to_discount = MAX_UNITS

Input.cart.line_items.each_with_index do |line_item, position|
  break if num_to_discount <= 0
  next unless line_item.variant.product.tags.include?("limited")

  if line_item.quantity > num_to_discount
    split_line_item = line_item.split(take: num_to_discount)
    Input.cart.line_items.insert(position + 1, split_line_item)
    line_item = split_line_item
  end

  line_item.change_line_price(
    line_item.line_price * (1.0 - (DISCOUNT_PERCENTAGE / 100.0)),
    message: "30% off your first 2"
  )
  num_to_discount -= line_item.quantity
end

Output.cart = Input.cart
何になるか

Discount on the first few units として認識され、bxgy タイプのオファーとして再構築されます。以下はオファーが実際に保存される設定キーそのものなので、ご自身の再構築結果を項目ごとに照合できます。

すべての設定項目
設定項目設定キー
Offer typebxgytype
Countingper_variantcounting
Recurrenceoncerecurrence
Tier 1min qty 1 -> free itemtiers[0]
Buy quantity1bxgy.buyQty
Get quantity2bxgy.getQty
Get selectionsame_itembxgy.getSelection
Reward on the get units30% offbxgy.getDiscountPct
Free units are additionalfalsebxgy.freeUnitsAreAdditional
Target scopetagstarget.scope
Target includeslimitedtarget.includeIds
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
確認を求められます
  • Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
次の点が表示されます
  • The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
それを裏づけるカート

公開デモストア Meridian Goods で作成しているため、ここに載せた価格はすべて、信じるのではなく再現して確かめられます。以下の割引額は、決済時に動くのと同じエンジンがこのカートに対して算出した金額です。

  • No Meridian Goods product carries the tag limited. Set it on Aria Wireless Earbuds in Shopify before you build the cart, and on nothing else.
このオファーが発動するカートと、その金額
商品数量単価
Aria Wireless Earbuds1$79.00
小計$79.00
割引-$23.70
合計$55.30

未ログイン、またはタグのない顧客としてログインしています。

ツールが照合する金額

公開前に、ツールは手元のカートに対して以前の Script が何をしていたかを尋ねます。カートを Aria Wireless Earbuds 1、単価 $79.00 に設定すれば、答えは一つに定まります。

割引額は $23.70 です。購入者が支払った合計は $55.30 です。

照合は通貨の最小単位まで完全一致で行われます。1 単位でもずれれば公開はできません。それがこの手順の目的です。

何も起きてはいけないカート
  • swap the cart for a single Braided USB-C Cable
このオファーが発動してはいけないカート
商品数量単価
Braided USB-C Cable1$14.00
小計$14.00
合計$14.00

未ログイン、またはタグのない顧客としてログインしています。

見た目がそっくりな 2 つのファイル

以下の各ペアは、同じ形のスクリプトを 2 通りに書いたものです。違いは 1 行か 2 行ですが、動作は異なります。どちらが手元のものか決める前に、両方を読んでください。

紛らわしいペア D5

Quantity limit first n discounted (D5)

再構築可

bxgy オファーとして再現されます。

このカートでの割引: $23.70

異なる行
    PercentageDiscount.new(30, "30% off first 2"),
Limit the discounted units を読む

Quantity limit cheapest n (D5)

再構築可

bxgy オファーとして再現されます。

このカートでの割引: $30.90

異なる行
    applicable_items = applicable_items.sort_by { |item| item.variant.price }
    PercentageDiscount.new(30, "30% off your 2 cheapest"),
Limit the discounted units を読む

紛らわしいペア D6

Quantity limit first n discounted (D6)

再構築可

bxgy オファーとして再現されます。

このカートでの割引: $23.70

異なる行
DISCOUNT_PERCENTAGE = 30
MAX_UNITS = 2
num_to_discount = MAX_UNITS
Input.cart.line_items.each_with_index do |line_item, position|
  break if num_to_discount <= 0
  next unless line_item.variant.product.tags.include?("limited")

さらに 9 行の違いが、それぞれのページにあります。

Limit the discounted units を読む

Quantity limit purchase cap (D6)

別の Shopify API

再現しません。This needs a different kind of Shopify app

異なる行
# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
class QuantityLimit < Campaign
  def initialize(condition, customer_qualifier, cart_qualifier, line_item_selector, limit_by, limit)
    super(condition, customer_qualifier, cart_qualifier, line_item_selector)
    @limit_by = limit_by
    @limit = limit

さらに 26 行の違いが、それぞれのページにあります。

Purchase quantity limit を読む

よくある質問

Will Stackable rebuild this script?
Yes. Every spelling on this page is rebuilt today as a bxgy 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 $23.70 off, and checkout charges $55.30.
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. The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.

関連する Script の型

Purchase quantity limit

A cap on how many units a shopper could buy, enforced by trimming the cart.

Buy one get one free

A set of paid units earns an extra unit at no charge.

Buy X get Y, inclusive

The same buy-and-get offer, counted so the reward sits inside each set rather than beside it.

Script を貼り付けて再構築を確認

再構築の結果は、公開する前に、以前の Script が請求していた金額と照合されます。

本サイトの運営には必須Cookieを使用しており、お客様の同意をいただいた場合のみ、アクセス状況を把握するための分析Cookieを使用します。詳細は Cookieポリシー.