Migrating a script that took an amount off each item
Written four different ways in the wild, and two of them differ only in whether the number in the file already meant cents. Reading a generator’s "5" as five cents is a hundredfold error, so all four spellings are shown side by side.
HerbouwdIt subtracted a fixed sum from every matching unit in the cart.
Merchants hebben dit op 4 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.
Amount off per unit (D1)
HerbouwdRiskant paar D1. 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 = Money.new(cents: 999)
Input.cart.line_items.each do |line_item|
next unless line_item.variant.product.product_type == "Clearance"
new_price = line_item.line_price - (DISCOUNT * line_item.quantity)
line_item.change_line_price(new_price, message: "$9.99 off each")
end
Output.cart = Input.cartHerkend als Amount off at a quantity 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 -> $9.99 off each | tiers[0] |
| Target scope | product_types | target.scope |
| Target includes | Clearance | target.includeIds |
| Method | automatic | discountMethod |
| Allocation | standard | stacking.allocation |
| Combines with other discounts | no | stacking |
- Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
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.
- No Meridian Goods product carries the type Clearance. Set it on Aria Wireless Earbuds in Shopify before you build the cart, and on nothing else.
| Artikel | Aantal | Stukprijs |
|---|---|---|
| Aria Wireless Earbuds | 1 | $79.00 |
| Subtotaal | $79.00 | |
| Korting | -$9.99 | |
| Totaal | $69.01 | |
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 Aria Wireless Earbuds voor $79.00 en het antwoord ligt vast.
De korting is $9.99. Het totaal dat de klant betaalde is $69.01.
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.
Amount off per unit (D7)
HerbouwdRiskant paar D7. Dit bestand en zijn tegenhanger zien er bijna hetzelfde uit en doen iets anders, dus lees ze allebei voordat je bepaalt welke jij hebt.
# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
class FixedItemDiscount
def initialize(amount, message)
@amount = Money.new(cents: 100) * amount
@message = message
end
def apply(line_item)
per_item_discount = @amount
discount_to_apply = [(per_item_discount * line_item.quantity), line_item.line_price].min
line_item.change_line_price([line_item.line_price - discount_to_apply, Money.zero].max, message: @message)
end
end
CAMPAIGNS = [
ConditionalDiscount.new(:all, nil, nil, nil, FixedItemDiscount.new(5, "$5 off each"), 0),
].freeze
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cartHerkend als Amount off at a quantity 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 -> $5.00 off each | 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 | -$5.00 | |
| Totaal | $9.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 1 van Braided USB-C Cable voor $14.00 en het antwoord ligt vast.
De korting is $5.00. Het totaal dat de klant betaalde is $9.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.
Amount off per unit dollars (D11)
HerbouwdRiskant paar D11. Dit bestand en zijn tegenhanger zien er bijna hetzelfde uit en doen iets anders, dus lees ze allebei voordat je bepaalt welke jij hebt.
# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
class FixedItemDiscount
def initialize(amount, message)
@amount = Money.new(cents: 100) * amount
@message = message
end
def apply(line_item)
discount_to_apply = [(@amount * line_item.quantity), line_item.line_price].min
line_item.change_line_price([line_item.line_price - discount_to_apply, Money.zero].max, message: @message)
end
end
CAMPAIGNS = [
ConditionalDiscount.new(:all, nil, nil, nil, FixedItemDiscount.new(5, "$5 off each"), 0),
].freeze
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cartHerkend als Amount off at a quantity 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 -> $5.00 off each | 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 | -$5.00 | |
| Totaal | $9.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 1 van Braided USB-C Cable voor $14.00 en het antwoord ligt vast.
De korting is $5.00. Het totaal dat de klant betaalde is $9.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.
Amount off per unit cents (D11)
HerbouwdRiskant paar D11. 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 = Money.new(cents: 500)
Input.cart.line_items.each do |line_item|
discount_to_apply = [(DISCOUNT * line_item.quantity), line_item.line_price].min
line_item.change_line_price([line_item.line_price - discount_to_apply, Money.zero].max, message: "$5 off each")
end
Output.cart = Input.cartHerkend als Amount off at a quantity 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 -> $5.00 off each | 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 | -$5.00 | |
| Totaal | $9.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 1 van Braided USB-C Cable voor $14.00 en het antwoord ligt vast.
De korting is $5.00. Het totaal dat de klant betaalde is $9.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 D1
Amount off per unit (D1)
HerbouwdOpnieuw gebouwd als volume-aanbieding.
Korting in de eigen winkelwagen: $9.99
DISCOUNT = Money.new(cents: 999)
new_price = line_item.line_price - (DISCOUNT * line_item.quantity)
line_item.change_line_price(new_price, message: "$9.99 off each")Fixed price per unit (D1)
HerbouwdOpnieuw gebouwd als volume-aanbieding.
Korting in de eigen winkelwagen: $69.01
FIXED_PRICE = Money.new(cents: 999)
line_item.change_line_price(FIXED_PRICE * line_item.quantity, message: "Clearance $9.99 each")Gevaarlijk paar D7
Amount off per unit (D7)
HerbouwdOpnieuw gebouwd als volume-aanbieding.
Korting in de eigen winkelwagen: $5.00
class FixedItemDiscount
def initialize(amount, message)
per_item_discount = @amount
discount_to_apply = [(per_item_discount * line_item.quantity), line_item.line_price].min
line_item.change_line_price([line_item.line_price - discount_to_apply, Money.zero].max, message: @message)
ConditionalDiscount.new(:all, nil, nil, nil, FixedItemDiscount.new(5, "$5 off each"), 0),Fixed total discount cart (D7)
HerbouwdOpnieuw gebouwd als spend_goal-aanbieding.
Korting in de eigen winkelwagen: $5.00
class FixedTotalDiscount
def initialize(amount, message, behaviour = :split)
@discount_applied = Money.zero
@behaviour = behaviour
return unless @discount_applied < @amount
discount_to_apply = [(@amount - @discount_applied), line_item.line_price].minEn nog 3 afwijkende regels, op de eigen pagina.
Gevaarlijk paar D11
Amount off per unit dollars (D11)
HerbouwdOpnieuw gebouwd als volume-aanbieding.
Korting in de eigen winkelwagen: $5.00
# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
class FixedItemDiscount
def initialize(amount, message)
@amount = Money.new(cents: 100) * amount
@message = message
endEn nog 10 afwijkende regels, op de eigen pagina.
Amount off per unit cents (D11)
HerbouwdOpnieuw gebouwd als volume-aanbieding.
Korting in de eigen winkelwagen: $5.00
DISCOUNT = Money.new(cents: 500)
Input.cart.line_items.each do |line_item|
discount_to_apply = [(DISCOUNT * line_item.quantity), line_item.line_price].min
line_item.change_line_price([line_item.line_price - discount_to_apply, Money.zero].max, message: "$5 off each")Veelgestelde vragen
- Will Stackable rebuild this script?
- Yes. Every spelling on this page is rebuilt today as a volume offer, and the cart above is the one that proves it.
- What cart proves it works?
- 1 x Aria Wireless Earbuds at $79.00. The engine takes $9.99 off, and checkout charges $69.01.
- What will I have to check before publishing?
- Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect. Confirm which products this applies to. We could not read product targeting from the script.
Verwante Script-vormen
Everything in the clearance line repriced to one round number per unit.
One lump sum off the whole cart, spread across the lines.
One percentage off the whole catalogue, with no condition attached.
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.