Migrating a percentage off shipping script
The shipping half of Shopify’s own dialect uses the same class name as the line-item half and stores the opposite fraction in it. One keeps what the shopper pays, the other keeps what comes off.
GenopbyggetIt reduced every delivery rate at checkout by a percentage.
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.
Selve dette Script
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 shipping'
# ================================ 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
discount_amount * 0.01
else
Money.new(cents: 100) * discount_amount
end
end
def apply(shipping_rate)
discount_amount = if @discount_type == :percent
shipping_rate.price * @discount_amount
else
@discount_amount > shipping_rate.price ? shipping_rate.price : @discount_amount
end
shipping_rate.apply_discount(discount_amount, message: @discount_message)
end
end
class DiscountRatesCampaign
def initialize(discount_applicator)
@discount_applicator = discount_applicator
end
def run(cart, shipping_rates)
shipping_rates.each do |shipping_rate|
@discount_applicator.apply(shipping_rate)
end
end
end
CAMPAIGNS = [
DiscountRatesCampaign.new(
DiscountApplicator.new(DISCOUNT_TYPE, DISCOUNT_AMOUNT, DISCOUNT_MESSAGE),
),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart, Input.shipping_rates)
end
Output.shipping_rates = Input.shipping_ratesGenkendt som Shipping discount og genopbygget som et tilbud af typen free_shipping. 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 | free_shipping | type |
| Counting | per_cart | counting |
| Recurrence | once | recurrence |
| Tier 1 | min spend $0.00 -> 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.
- This discounts every shipping rate at checkout, not one named rate.
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 | |
| Levering, normalt $6.90 | $6.21 | |
| Total | $14.00 | |
Logget ud, eller logget ind som kunde uden tags.
This rebuild only changes shipping. We price shipping against a sample rate rather than the rate your shopper saw, so there is no figure here we could honestly compare. Publishing records that the amount was not checked.
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, 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.
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.
Ofte stillede spørgsmål
- Will Stackable rebuild this script?
- Yes. Every spelling on this page is rebuilt today as a free_shipping offer, and the cart above is the one that proves it.
- What cart proves it works?
- 1 x Braided USB-C Cable at $14.00. The engine takes $0.69 off, and checkout charges $14.00 plus $6.21 delivery.
- What will I have to check before publishing?
- Confirm which products this applies to. We could not read product targeting from the script. This discounts every shipping rate at checkout, not one named rate.
Beslægtede Script-typer
The dialect Shopify’s own documentation taught, with its settings block above a do-not-edit banner.
A flat sum off the delivery charge, floored at free.
Every delivery rate rewritten to one price rather than reduced by an amount.
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.