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.
HerbouwdIt read the merchant-edited constant at the top of the file and applied that discount to every matching line.
Merchants hebben dit op 2 verschillende manieren geschreven, en die betekenen niet allemaal hetzelfde. Elke schrijfwijze staat hieronder met een eigen winkelwagen en eigen cijfers.
Plak het in Scripts-redding en kijk wat eruit komt
- Kopieer de Ruby met de knop naast elk Script op deze pagina.
- Open in je Shopify-beheer Stackable, dan Hulpmiddelen, dan Scripts-redding.
- Plak het daar, lees wat het hulpmiddel zegt te gaan bouwen en vergelijk dat met de instellingen op deze pagina.
Scripts-redding zit in het Pro-abonnement. Er wordt niets gepubliceerd totdat je vertelt wat je oude Script gaf en beide bedragen tot op de cent kloppen.
Official dialect, tiered spend
Herbouwd# ================================ 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.cartHerkend als Spend threshold discount en herbouwd als aanbieding van het type spend_goal. Dit zijn de echte configuratiesleutels waarmee de aanbieding wordt opgeslagen, zodat je je eigen herbouw veld voor veld kunt nalopen.
| Instelling | Waarde | Configuratiesleutel |
|---|---|---|
| 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.
Gebouwd in onze openbare demowinkel, Meridian Goods, zodat elke prijs hier na te rekenen is in plaats van op vertrouwen aangenomen. De korting hieronder is wat dezelfde engine die bij het afrekenen draait, voor precies deze winkelwagen berekent.
| Artikel | Aantal | Stukprijs |
|---|---|---|
| Softcover Notebook Set | 1 | $18.00 |
| Braided USB-C Cable | 1 | $14.00 |
| Subtotaal | $32.00 | |
| Korting | -$3.20 | |
| Totaal | $28.80 | |
Uitgelogd, of ingelogd als klant zonder tags.
Voor het publiceren vraagt de tool wat je oude Script deed met een winkelwagen die je al hebt. Zet die op 2 van Softcover Notebook Set voor $18.00 en het antwoord ligt vast.
De korting is $3.60. Het totaal dat de klant betaalde is $32.40.
De vergelijking is exact, tot op de kleinste eenheid van je valuta. Eén cent ernaast en publiceren blijft dicht, en daar gaat deze stap juist om.
- swap the cart for a single Braided USB-C Cable
| Artikel | Aantal | Stukprijs |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Subtotaal | $14.00 | |
| Totaal | $14.00 | |
Uitgelogd, of ingelogd als klant zonder tags.
Official dialect, lineitem applicator (D16)
HerbouwdRiskant paar D16. Dit bestand en zijn tegenhanger zien er bijna hetzelfde uit en doen iets anders, dus lees ze allebei voordat je bepaalt welke jij hebt.
# ================================ 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.cartHerkend als Percentage off en herbouwd als aanbieding van het type volume. Dit zijn de echte configuratiesleutels waarmee de aanbieding wordt opgeslagen, zodat je je eigen herbouw veld voor veld kunt nalopen.
| Instelling | Waarde | Configuratiesleutel |
|---|---|---|
| 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.
Gebouwd in onze openbare demowinkel, Meridian Goods, zodat elke prijs hier na te rekenen is in plaats van op vertrouwen aangenomen. De korting hieronder is wat dezelfde engine die bij het afrekenen draait, voor precies deze winkelwagen berekent.
| Artikel | Aantal | Stukprijs |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Subtotaal | $14.00 | |
| Korting | -$1.40 | |
| Totaal | $12.60 | |
Uitgelogd, of ingelogd als klant zonder tags.
Voor het publiceren vraagt de tool wat je oude Script deed met een winkelwagen die je al hebt. Zet die op 1 van Braided USB-C Cable voor $14.00 en het antwoord ligt vast.
De korting is $1.40. Het totaal dat de klant betaalde is $12.60.
De vergelijking is exact, tot op de kleinste eenheid van je valuta. Eén cent ernaast en publiceren blijft dicht, en daar gaat deze stap juist om.
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.
Twee bestanden die op elkaar lijken
Elk paar hieronder is dezelfde Script-vorm, op twee manieren geschreven. Ze schelen een of twee regels en doen iets anders, dus lees ze allebei voordat je bepaalt welke jij hebt.
Gevaarlijk paar D16
Official dialect, lineitem applicator (D16)
HerbouwdOpnieuw gebouwd als volume-aanbieding.
Korting in de eigen winkelwagen: $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].maxEn nog 8 afwijkende regels, op de eigen pagina.
Official dialect, shipping applicator (D16)
HerbouwdOpnieuw gebouwd als free_shipping-aanbieding.
Verzendvoordeel in de eigen winkelwagen: $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_amountEn nog 8 afwijkende regels, op de eigen pagina.
Veelgestelde vragen
- 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.
Verwante Script-vormen
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.
Plak je Script en zie het herbouwd worden
De herbouw wordt getoetst aan het bedrag dat je oude Script rekende voordat je hem kunt publiceren.