Migrating a percentage off the cheapest item script
Shopify shipped this one in the picker, so it is widely copied, and almost every copy carries the same gate: more than one cart LINE. Two of the same item on one line is two units and one line, and the original stayed silent on it.
HerbouwdIt found the cheapest line in the cart and took a percentage off one unit of it.
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.
Cheapest item percentage line count (D15)
HerbouwdRiskant paar D15. 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_AMOUNT = 50
if (Input.cart.line_items.size > 1)
line_item = Input.cart.line_items.sort_by { |line_item| line_item.variant.price }.first
if line_item.quantity > 1
line_item = line_item.split(take: 1)
Input.cart.line_items << line_item
end
line_item.change_line_price(
line_item.line_price * (1.0 - (DISCOUNT_AMOUNT / 100.0)),
message: "50% off your least expensive item"
)
end
Output.cart = Input.cartHerkend als Cheapest item discounted en herbouwd als aanbieding van het type bxgy. 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 | bxgy | type |
| Counting | per_group | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 2 -> free item | tiers[0] |
| Buy quantity | 2 | bxgy.buyQty |
| Get quantity | 1 | bxgy.getQty |
| Get selection | cheapest_eligible | bxgy.getSelection |
| Reward on the get units | 50% off | bxgy.getDiscountPct |
| Free units are additional | false | bxgy.freeUnitsAreAdditional |
| Target scope | all | target.scope |
| Method | automatic | discountMethod |
| Allocation | standard | stacking.allocation |
| Combines with other discounts | no | stacking |
- Your Script counted separate cart lines. We count units, so one line holding two items now qualifies where it did not before. Confirm the buy requirement.
- Confirm which products this applies to. We could not read product targeting from the script.
- Your Script needed two separate cart lines. This offer counts units, so two of the same item on one line now qualifies.
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 | -$7.00 | |
| Totaal | $25.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 2 van Braided USB-C Cable voor $14.00 en het antwoord ligt vast.
De korting is $7.00. Het totaal dat de klant betaalde is $21.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.
- 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.
Cheapest item percentage unit count (D15)
HerbouwdRiskant paar D15. 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_AMOUNT = 50
total_units = Input.cart.line_items.map(&:quantity).reduce(0, :+)
if total_units >= 2
line_item = Input.cart.line_items.sort_by { |li| li.variant.price }.first
if line_item.quantity > 1
line_item = line_item.split(take: 1)
Input.cart.line_items << line_item
end
line_item.change_line_price(
line_item.line_price * (1.0 - (DISCOUNT_AMOUNT / 100.0)),
message: "50% off your least expensive item"
)
end
Output.cart = Input.cartHerkend als Cheapest item discounted en herbouwd als aanbieding van het type bxgy. 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 | bxgy | type |
| Counting | per_group | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 2 -> free item | tiers[0] |
| Buy quantity | 2 | bxgy.buyQty |
| Get quantity | 1 | bxgy.getQty |
| Get selection | cheapest_eligible | bxgy.getSelection |
| Reward on the get units | 50% off | bxgy.getDiscountPct |
| Free units are additional | false | bxgy.freeUnitsAreAdditional |
| 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 | -$7.00 | |
| Totaal | $25.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 2 van Braided USB-C Cable voor $14.00 en het antwoord ligt vast.
De korting is $7.00. Het totaal dat de klant betaalde is $21.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.
- 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.
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 D15
Cheapest item percentage line count (D15)
HerbouwdOpnieuw gebouwd als bxgy-aanbieding.
Korting in de eigen winkelwagen: $7.00
if (Input.cart.line_items.size > 1)
line_item = Input.cart.line_items.sort_by { |line_item| line_item.variant.price }.firstCheapest item percentage unit count (D15)
HerbouwdOpnieuw gebouwd als bxgy-aanbieding.
Korting in de eigen winkelwagen: $7.00
total_units = Input.cart.line_items.map(&:quantity).reduce(0, :+)
if total_units >= 2
line_item = Input.cart.line_items.sort_by { |li| li.variant.price }.firstVeelgestelde vragen
- Will Stackable rebuild this script?
- Yes. Every spelling on this page is rebuilt today as a bxgy 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 $7.00 off, and checkout charges $25.00.
- What will I have to check before publishing?
- Your Script counted separate cart lines. We count units, so one line holding two items now qualifies where it did not before. Confirm the buy requirement. Confirm which products this applies to. We could not read product targeting from the script. Your Script needed two separate cart lines. This offer counts units, so two of the same item on one line now qualifies.
Verwante Script-vormen
A set of paid units earns an extra unit at no charge.
The same buy-and-get offer, counted so the reward sits inside each set rather than beside it.
The discount applied to the first few units only, with the rest at full price.
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.