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.
GenopbyggetIt read the merchant-edited constant at the top of the file and applied that discount to every matching line.
Handlende har skrevet det her på 2 forskellige måder, og de betyder ikke alle det samme. Hver variant står nedenfor med sin egen kurv og sine egne tal.
Indsæt det i Scripts-redning, og se hvad der bliver bygget
- Kopiér Ruby-koden med knappen ved siden af ethvert Script på siden.
- Åbn Stackable i Shopify-administrationen, derefter Værktøjer og så Scripts-redning.
- Indsæt det der, læs hvad værktøjet siger, det vil bygge, og hold det op mod indstillingerne på denne side.
Scripts-redning hører til Pro-abonnementet. Intet bliver udgivet, før du oplyser, hvad dit gamle Script gav i rabat, og de to beløb stemmer helt ned på øret.
Official dialect, tiered spend
Genopbygget# ================================ 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.cartGenkendt som Spend threshold discount og genopbygget som et tilbud af typen spend_goal. Det her er de rigtige konfigurationsnøgler, som tilbuddet gemmes med, så du kan tjekke din egen genopbygning felt for felt.
| Indstilling | Værdi | Konfigurationsnøgle |
|---|---|---|
| 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.
Bygget i vores offentlige demobutik, Meridian Goods, så hver pris her kan efterregnes i stedet for at blive taget på tro. Rabatten nedenfor er det, som den samme motor, der kører i kassen, beregner for præcis denne kurv.
| Vare | Antal | Stykpris |
|---|---|---|
| Softcover Notebook Set | 1 | $18.00 |
| Braided USB-C Cable | 1 | $14.00 |
| Subtotal | $32.00 | |
| Rabat | -$3.20 | |
| Total | $28.80 | |
Logget ud, eller logget ind som kunde uden tags.
Inden du udgiver, spørger værktøjet, hvad dit gamle Script gjorde ved en kurv, du allerede har. Sæt den til 2 af Softcover Notebook Set à $18.00, så ligger svaret fast.
Rabatten er $3.60. Det beløb, kunden betalte, er $32.40.
Sammenligningen er præcis, helt ned til den mindste enhed i din valuta. En øre ved siden af, og udgivelsen forbliver spærret, hvilket er hele pointen med trinnet.
- swap the cart for a single Braided USB-C Cable
| Vare | Antal | Stykpris |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Subtotal | $14.00 | |
| Total | $14.00 | |
Logget ud, eller logget ind som kunde uden tags.
Official dialect, lineitem applicator (D16)
GenopbyggetFarligt par D16. Denne fil og dens makker ser næsten ens ud og opfører sig forskelligt, så læs dem begge, før du afgør, hvilken du har.
# ================================ 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.cartGenkendt som Percentage off og genopbygget som et tilbud af typen volume. Det her er de rigtige konfigurationsnøgler, som tilbuddet gemmes med, så du kan tjekke din egen genopbygning felt for felt.
| Indstilling | Værdi | Konfigurationsnøgle |
|---|---|---|
| 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.
Bygget i vores offentlige demobutik, Meridian Goods, så hver pris her kan efterregnes i stedet for at blive taget på tro. Rabatten nedenfor er det, som den samme motor, der kører i kassen, beregner for præcis denne kurv.
| Vare | Antal | Stykpris |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Subtotal | $14.00 | |
| Rabat | -$1.40 | |
| Total | $12.60 | |
Logget ud, eller logget ind som kunde uden tags.
Inden du udgiver, spørger værktøjet, hvad dit gamle Script gjorde ved en kurv, du allerede har. Sæt den til 1 af Braided USB-C Cable à $14.00, så ligger svaret fast.
Rabatten er $1.40. Det beløb, kunden betalte, er $12.60.
Sammenligningen er præcis, helt ned til den mindste enhed i din valuta. En øre ved siden af, og udgivelsen forbliver spærret, hvilket er hele pointen med trinnet.
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.
To filer, der ligner hinanden
Hvert par herunder er den samme Script-form skrevet på to måder. De adskiller sig på en eller to linjer og gør noget forskelligt, så læs dem begge, før du afgør, hvilken du har.
Farligt par D16
Official dialect, lineitem applicator (D16)
GenopbyggetBygges om til et volume-tilbud.
Rabat i dens egen kurv: $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].maxOg 8 flere forskellige linjer, på dens egen side.
Official dialect, shipping applicator (D16)
GenopbyggetBygges om til et free_shipping-tilbud.
Fragtbesparelse i dens egen kurv: $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_amountOg 8 flere forskellige linjer, på dens egen side.
Ofte stillede spørgsmål
- 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.
Beslægtede Script-typer
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.
Indsæt dit Script og se det blive genopbygget
Genopbygningen holdes op mod det beløb, dit gamle Script opkrævede, før du kan udgive den.