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

Migrating a Shopify Help Center script template

Every example script Shopify published shared one skeleton: a settings constant the merchant edited, a banner comment, then class code nobody was meant to touch. If your file opens with a row of equals signs, this is the dialect you have.

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

It read the merchant-edited constant at the top of the file and applied that discount to every matching line.

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

自分のストアで試す

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

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

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

Official dialect, tiered spend

再構築可
元の Ruby

# ================================ Customizable Settings ================================
# ================================================================
# Tiered Discount by Spend
#
#   If the cart total is greater than (or equal to) an entered
#   threshold, the associated discount is applied to each item.
# ================================================================
SPENDING_THRESHOLDS = [
  {
    threshold: 30,
    discount_type: :percent,
    discount_amount: 10,
    discount_message: '10% off for spending over $30',
  },
  {
    threshold: 60,
    discount_type: :percent,
    discount_amount: 15,
    discount_message: '15% off for spending over $60',
  },
]

# ================================ Script Code (do not edit) ================================
class DiscountApplicator
  def initialize(discount_type, discount_amount, discount_message)
    @discount_type = discount_type
    @discount_message = discount_message

    @discount_amount = if discount_type == :percent
      1 - (discount_amount * 0.01)
    else
      Money.new(cents: 100) * discount_amount
    end
  end

  def apply(line_item)
    new_line_price = if @discount_type == :percent
      line_item.line_price * @discount_amount
    else
      [line_item.line_price - (@discount_amount * line_item.quantity), Money.zero].max
    end

    line_item.change_line_price(new_line_price, message: @discount_message)
  end
end

class TieredDiscountBySpendCampaign
  def initialize(campaigns)
    @campaigns = campaigns
  end

  def run(cart)
    applicable_campaign = nil

    @campaigns.each do |campaign|
      threshold = Money.new(cents: 100) * campaign[:threshold]
      applicable_campaign = campaign if cart.subtotal_price >= threshold
    end

    return if applicable_campaign.nil?

    discount_applicator = DiscountApplicator.new(
      applicable_campaign[:discount_type],
      applicable_campaign[:discount_amount],
      applicable_campaign[:discount_message],
    )

    cart.line_items.each do |line_item|
      discount_applicator.apply(line_item)
    end
  end
end

CAMPAIGNS = [
  TieredDiscountBySpendCampaign.new(SPENDING_THRESHOLDS),
]

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

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

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

すべての設定項目
設定項目設定キー
Offer typespend_goaltype
Countingper_cartcounting
Recurrenceoncerecurrence
Tier 1min spend $30.00 -> 10% offtiers[0]
Tier 2min spend $60.00 -> 15% offtiers[1]
Target scopealltarget.scope
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
確認を求められます
  • Confirm which products this applies to. We could not read product targeting from the script.
それを裏づけるカート

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

このオファーが発動するカートと、その金額
商品数量単価
Softcover Notebook Set1$18.00
Braided USB-C Cable1$14.00
小計$32.00
割引-$3.20
合計$28.80

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

ツールが照合する金額

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

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

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

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

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

Official dialect, lineitem applicator (D16)

再構築可

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

元の Ruby

# ================================ Customizable Settings ================================
DISCOUNT_TYPE = :percent
DISCOUNT_AMOUNT = 10
DISCOUNT_MESSAGE = '10% off everything'

# ================================ Script Code (do not edit) ================================
class DiscountApplicator
  def initialize(discount_type, discount_amount, discount_message)
    @discount_type = discount_type
    @discount_message = discount_message

    @discount_amount = if discount_type == :percent
      1 - (discount_amount * 0.01)
    else
      Money.new(cents: 100) * discount_amount
    end
  end

  def apply(line_item)
    new_line_price = if @discount_type == :percent
      line_item.line_price * @discount_amount
    else
      [line_item.line_price - (@discount_amount * line_item.quantity), Money.zero].max
    end

    line_item.change_line_price(new_line_price, message: @discount_message)
  end
end

class ProductDiscountCampaign
  def initialize(discount_applicator)
    @discount_applicator = discount_applicator
  end

  def run(cart)
    cart.line_items.each do |line_item|
      @discount_applicator.apply(line_item)
    end
  end
end

CAMPAIGNS = [
  ProductDiscountCampaign.new(
    DiscountApplicator.new(DISCOUNT_TYPE, DISCOUNT_AMOUNT, DISCOUNT_MESSAGE),
  ),
]

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

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

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

すべての設定項目
設定項目設定キー
Offer typevolumetype
Countingper_productcounting
Recurrenceoncerecurrence
Tier 1min qty 1 -> 10% offtiers[0]
Target scopealltarget.scope
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
確認を求められます
  • Confirm which products this applies to. We could not read product targeting from the script.
それを裏づけるカート

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

このオファーが発動するカートと、その金額
商品数量単価
Braided USB-C Cable1$14.00
小計$14.00
割引-$1.40
合計$12.60

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

ツールが照合する金額

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

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

照合は通貨の最小単位まで完全一致で行われます。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 行ですが、動作は異なります。どちらが手元のものか決める前に、両方を読んでください。

紛らわしいペア D16

Official dialect, lineitem applicator (D16)

再構築可

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

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

異なる行
DISCOUNT_MESSAGE = '10% off everything'
      1 - (discount_amount * 0.01)
  def apply(line_item)
    new_line_price = if @discount_type == :percent
      line_item.line_price * @discount_amount
      [line_item.line_price - (@discount_amount * line_item.quantity), Money.zero].max

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

Help Center script template を読む

Official dialect, shipping applicator (D16)

再構築可

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

このカートでの配送料の節約: $0.69

異なる行
DISCOUNT_MESSAGE = '10% off shipping'
      discount_amount * 0.01
  def apply(shipping_rate)
    discount_amount = if @discount_type == :percent
      shipping_rate.price * @discount_amount
      @discount_amount > shipping_rate.price ? shipping_rate.price : @discount_amount

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

Percentage off shipping を読む

よくある質問

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 Softcover Notebook Set at $18.00 plus 1 x Braided USB-C Cable at $14.00. The engine takes $3.20 off, and checkout charges $28.80.
What will I have to check before publishing?
Confirm which products this applies to. We could not read product targeting from the script.

関連する Script の型

Percentage off shipping

A share taken off every delivery rate at checkout.

Script Creator output

Generator output, with a CAMPAIGNS array at the bottom that states the merchant’s intent as data.

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

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

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