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.
NachgebautIt read the merchant-edited constant at the top of the file and applied that discount to every matching line.
Händler haben das auf 2 verschiedene Arten geschrieben, und sie bedeuten nicht alle dasselbe. Jede Schreibweise steht unten mit eigenem Warenkorb und eigenen Zahlen.
Fügen Sie es in die Scripts-Rettung ein und sehen Sie, was daraus wird
- Kopieren Sie das Ruby mit der Schaltfläche neben jedem Script auf dieser Seite.
- Öffnen Sie im Shopify-Adminbereich Stackable, dann Tools, dann Scripts-Rettung.
- Fügen Sie es dort ein, lesen Sie, was das Tool bauen will, und vergleichen Sie das mit den Einstellungen auf dieser Seite.
Die Scripts-Rettung gehört zum Pro-Tarif. Nichts wird veröffentlicht, bevor Sie angeben, was Ihr altes Script berechnet hat, und beide Zahlen auf den Cent genau übereinstimmen.
Official dialect, tiered spend
Nachgebaut# ================================ 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.cartErkannt als Spend threshold discount und neu aufgebaut als Angebot vom Typ spend_goal. Das sind die echten Konfigurationsschlüssel, mit denen das Angebot gespeichert wird, sodass Sie Ihren eigenen Nachbau Feld für Feld damit abgleichen können.
| Einstellung | Wert | Konfigurationsschlüssel |
|---|---|---|
| Offer type | spend_goal | type |
| Counting | per_cart | counting |
| Recurrence | once | recurrence |
| Tier 1 | min spend $30.00 -> 10% off | tiers[0] |
| Tier 2 | min spend $60.00 -> 15% off | tiers[1] |
| 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.
Aufgebaut in unserem öffentlichen Demo-Shop Meridian Goods, damit sich jeder Preis hier nachrechnen lässt und nicht geglaubt werden muss. Der Rabatt unten ist das, was dieselbe Engine, die im Checkout läuft, für genau diesen Warenkorb berechnet.
| Artikel | Menge | Einzelpreis |
|---|---|---|
| Softcover Notebook Set | 1 | $18.00 |
| Braided USB-C Cable | 1 | $14.00 |
| Zwischensumme | $32.00 | |
| Rabatt | -$3.20 | |
| Summe | $28.80 | |
Nicht angemeldet oder als Kunde ohne Tags angemeldet.
Vor dem Veröffentlichen fragt das Tool, was Ihr altes Script mit einem Warenkorb gemacht hat, den Sie bereits haben. Stellen Sie ihn auf 2 von Softcover Notebook Set zu $18.00 ein, dann steht die Antwort fest.
Der Rabatt beträgt $3.60. Der Betrag, den der Käufer gezahlt hat, ist $32.40.
Der Vergleich ist exakt, bis auf die kleinste Einheit Ihrer Währung. Ein Cent daneben und das Veröffentlichen bleibt gesperrt, genau darum geht es bei diesem Schritt.
- swap the cart for a single Braided USB-C Cable
| Artikel | Menge | Einzelpreis |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Zwischensumme | $14.00 | |
| Summe | $14.00 | |
Nicht angemeldet oder als Kunde ohne Tags angemeldet.
Official dialect, lineitem applicator (D16)
NachgebautGefährliches Paar D16. Diese Datei und ihr Gegenstück sehen fast gleich aus und verhalten sich unterschiedlich, lesen Sie also beide, bevor Sie entscheiden, welche Sie haben.
# ================================ 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.cartErkannt als Percentage off und neu aufgebaut als Angebot vom Typ volume. Das sind die echten Konfigurationsschlüssel, mit denen das Angebot gespeichert wird, sodass Sie Ihren eigenen Nachbau Feld für Feld damit abgleichen können.
| Einstellung | Wert | Konfigurationsschlüssel |
|---|---|---|
| Offer type | volume | type |
| Counting | per_product | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 1 -> 10% off | 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.
Aufgebaut in unserem öffentlichen Demo-Shop Meridian Goods, damit sich jeder Preis hier nachrechnen lässt und nicht geglaubt werden muss. Der Rabatt unten ist das, was dieselbe Engine, die im Checkout läuft, für genau diesen Warenkorb berechnet.
| Artikel | Menge | Einzelpreis |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Zwischensumme | $14.00 | |
| Rabatt | -$1.40 | |
| Summe | $12.60 | |
Nicht angemeldet oder als Kunde ohne Tags angemeldet.
Vor dem Veröffentlichen fragt das Tool, was Ihr altes Script mit einem Warenkorb gemacht hat, den Sie bereits haben. Stellen Sie ihn auf 1 von Braided USB-C Cable zu $14.00 ein, dann steht die Antwort fest.
Der Rabatt beträgt $1.40. Der Betrag, den der Käufer gezahlt hat, ist $12.60.
Der Vergleich ist exakt, bis auf die kleinste Einheit Ihrer Währung. Ein Cent daneben und das Veröffentlichen bleibt gesperrt, genau darum geht es bei diesem Schritt.
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.
Zwei Dateien, die gleich aussehen
Jedes Paar unten ist dieselbe Script-Form in zwei Schreibweisen. Sie unterscheiden sich in ein, zwei Zeilen und tun Unterschiedliches. Lesen Sie beide, bevor Sie entscheiden, welche Sie haben.
Gefährliches Paar D16
Official dialect, lineitem applicator (D16)
NachgebautWird als volume-Angebot nachgebaut.
Rabatt im eigenen Warenkorb: $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].maxUnd 8 weitere abweichende Zeilen, auf der eigenen Seite.
Official dialect, shipping applicator (D16)
NachgebautWird als free_shipping-Angebot nachgebaut.
Versandersparnis im eigenen Warenkorb: $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_amountUnd 8 weitere abweichende Zeilen, auf der eigenen Seite.
Häufige Fragen
- 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.
Verwandte Script-Formen
A share taken off every delivery rate at checkout.
Generator output, with a CAMPAIGNS array at the bottom that states the merchant’s intent as data.
Fügen Sie Ihr Script ein und sehen Sie den Nachbau
Der Nachbau wird mit dem Betrag abgeglichen, den Ihr altes Script berechnet hat, bevor Sie ihn veröffentlichen können.