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.
HerbouwdIt worked out a percentage of the cart and then refused to take off more than a stated maximum.
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.
Het Script
HerbouwdRiskant paar D10. Dit bestand en zijn tegenhanger zien er bijna hetzelfde uit en doen iets anders, dus lees ze allebei voordat je bepaalt welke jij hebt.
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.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 $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.
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 |
|---|---|---|
| Canvas Belt Bag | 1 | $42.00 |
| Aria Wireless Earbuds | 2 | $79.00 |
| Subtotaal | $200.00 | |
| Korting | -$50.00 | |
| Totaal | $150.00 | |
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 8 van Walnut Desk Organizer voor $26.00 en het antwoord ligt vast.
De korting is $50.00. Het totaal dat de klant betaalde is $158.00.
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 D10
Capped percentage discount (D10)
HerbouwdOpnieuw gebouwd als spend_goal-aanbieding.
Korting in de eigen winkelwagen: $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)En nog 2 afwijkende regels, op de eigen pagina.
Fixed total discount cart (D10)
HerbouwdOpnieuw gebouwd als spend_goal-aanbieding.
Korting in de eigen winkelwagen: $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 - shareVeelgestelde 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 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.
Verwante Script-vormen
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.
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.