Migrating a capped percentage discount script
Common on high-value carts and the one place where getting it wrong is wrong in both directions at once: read the cap as the discount and you give $50 flat, drop the cap and you give an uncapped quarter of the cart.
OmbyggtIt worked out a percentage of the cart and then refused to take off more than a stated maximum.
Klistra in det i Scripts-räddning och se vad som byggs
- Kopiera Ruby-koden med knappen bredvid vilket Script som helst på sidan.
- Öppna Stackable i Shopify-adminen, sedan Verktyg, sedan Scripts-räddning.
- Klistra in det där, läs vad verktyget säger att det ska bygga och jämför med inställningarna på den här sidan.
Scripts-räddning ingår i Pro-planen. Ingenting publiceras förrän du anger vad ditt gamla Script gav i rabatt och båda beloppen stämmer på öret.
Detta Script
OmbyggtRiskabelt par D10. Den här filen och dess motpart ser nästan likadana ut och beter sig olika, så läs båda innan du avgör vilken du har.
DISCOUNT_PERCENTAGE = 25
MAX_DISCOUNT = 50
cart_subtotal_float = Float((Input.cart.subtotal_price.cents / 100).to_s)
raw_discount = cart_subtotal_float * (DISCOUNT_PERCENTAGE / 100.0)
total_discount = [raw_discount, MAX_DISCOUNT].min
Input.cart.line_items.each do |line_item|
line_price_float = Float((line_item.line_price.cents / 100).to_s)
percentage = 1 - (((line_price_float / cart_subtotal_float) * total_discount) / line_price_float)
line_item.change_line_price(line_item.line_price * percentage, message: "25% off, up to $50")
end
Output.cart = Input.cartIdentifierat som Spend threshold discount och ombyggt till ett erbjudande av typen spend_goal. Det här är de verkliga konfigurationsnycklarna som erbjudandet sparas med, så du kan stämma av din egen ombyggnad fält för fält.
| Inställning | Värde | Konfigurationsnyckel |
|---|---|---|
| Offer type | spend_goal | type |
| Counting | per_cart | counting |
| Recurrence | once | recurrence |
| Tier 1 | min spend $0.00 -> 25% off | tiers[0] |
| Target scope | all | target.scope |
| Maximum discount | $50.00 | maxDiscountAmount |
| 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.
- The maximum applies to the whole order and is shared across the discounted items. If your Script capped each line on its own, that gave more away than this does.
Byggd i vår publika demobutik, Meridian Goods, så att varje pris här går att räkna efter i stället för att tas på förtroende. Rabatten nedan är vad samma motor som kör i kassan räknar fram för exakt den här varukorgen.
| Artikel | Antal | Styckpris |
|---|---|---|
| Canvas Belt Bag | 1 | $42.00 |
| Aria Wireless Earbuds | 2 | $79.00 |
| Delsumma | $200.00 | |
| Rabatt | -$50.00 | |
| Summa | $150.00 | |
Utloggad, eller inloggad som kund utan taggar.
Före publicering frågar verktyget vad ditt gamla Script gjorde med en varukorg du redan har. Sätt den till 8 av Walnut Desk Organizer à $26.00, så är svaret givet.
Rabatten är $50.00. Summan som kunden betalade är $158.00.
Jämförelsen är exakt, ned till minsta enhet i din valuta. Ett öre fel och publiceringen förblir stängd, vilket är hela poängen med steget.
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.
Två filer som ser likadana ut
Varje par nedan är samma Script-form skriven på två sätt. De skiljer sig åt på en eller två rader och gör olika saker, så läs båda innan du avgör vilken du har.
Farligt par D10
Capped percentage discount (D10)
OmbyggtByggs om till ett spend_goal-erbjudande.
Rabatt i sin egen varukorg: $50.00
DISCOUNT_PERCENTAGE = 25
MAX_DISCOUNT = 50
cart_subtotal_float = Float((Input.cart.subtotal_price.cents / 100).to_s)
raw_discount = cart_subtotal_float * (DISCOUNT_PERCENTAGE / 100.0)
total_discount = [raw_discount, MAX_DISCOUNT].min
line_price_float = Float((line_item.line_price.cents / 100).to_s)Och 2 rader till som skiljer sig, på den egna sidan.
Fixed total discount cart (D10)
OmbyggtByggs om till ett spend_goal-erbjudande.
Rabatt i sin egen varukorg: $14.00
MAX_DISCOUNT = Money.new(cents: 10000)
remaining = MAX_DISCOUNT
break if remaining <= Money.zero
share = [line_item.line_price, remaining].min
line_item.change_line_price(line_item.line_price - share, message: "$100 off your order")
remaining = remaining - shareVanliga frågor
- 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 Canvas Belt Bag at $42.00 plus 2 x Aria Wireless Earbuds at $79.00. The engine takes $50.00 off, and checkout charges $150.00.
- What will I have to check before publishing?
- Confirm which products this applies to. We could not read product targeting from the script. The maximum applies to the whole order and is shared across the discounted items. If your Script capped each line on its own, that gave more away than this does.
Besläktade Script-former
One lump sum off the whole cart, spread across the lines.
Everything in the clearance line repriced to one round number per unit.
A flat sum taken off every unit, however many the shopper bought.
Klistra in ditt Script och se det byggas om
Ombyggnaden stäms av mot beloppet som ditt gamla Script tog ut innan du kan publicera den.