Naar de inhoud
Script-voorbeelden

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.

Herbouwd
Wat het Script deed

It 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.

Probeer het in je eigen winkel

Plak het in Scripts-redding en kijk wat eruit komt

  1. Kopieer de Ruby met de knop naast elk Script op deze pagina.
  2. Open in je Shopify-beheer Stackable, dan Hulpmiddelen, dan Scripts-redding.
  3. 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)

Herbouwd

Riskant 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.

De oorspronkelijke Ruby

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.cart
Wat het wordt

Herkend 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.

Elke instelling, uitgeschreven
InstellingWaardeConfiguratiesleutel
Offer typebxgytype
Countingper_groupcounting
Recurrenceoncerecurrence
Tier 1min qty 2 -> free itemtiers[0]
Buy quantity2bxgy.buyQty
Get quantity1bxgy.getQty
Get selectioncheapest_eligiblebxgy.getSelection
Reward on the get units50% offbxgy.getDiscountPct
Free units are additionalfalsebxgy.freeUnitsAreAdditional
Target scopealltarget.scope
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
Er wordt om bevestiging gevraagd
  • 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.
Je krijgt te horen
  • Your Script needed two separate cart lines. This offer counts units, so two of the same item on one line now qualifies.
De winkelwagen die het bewijst

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.

De winkelwagen die deze aanbieding activeert, en wat die kost
ArtikelAantalStukprijs
Softcover Notebook Set1$18.00
Braided USB-C Cable1$14.00
Subtotaal$32.00
Korting-$7.00
Totaal$25.00

Uitgelogd, of ingelogd als klant zonder tags.

Het bedrag waaraan de tool je toetst

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.

De winkelwagen die niets mag opleveren
  • swap the cart for a single Braided USB-C Cable
De winkelwagen die deze aanbieding niet mag activeren
ArtikelAantalStukprijs
Braided USB-C Cable1$14.00
Subtotaal$14.00
Totaal$14.00

Uitgelogd, of ingelogd als klant zonder tags.

Cheapest item percentage unit count (D15)

Herbouwd

Riskant 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.

De oorspronkelijke Ruby

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.cart
Wat het wordt

Herkend 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.

Elke instelling, uitgeschreven
InstellingWaardeConfiguratiesleutel
Offer typebxgytype
Countingper_groupcounting
Recurrenceoncerecurrence
Tier 1min qty 2 -> free itemtiers[0]
Buy quantity2bxgy.buyQty
Get quantity1bxgy.getQty
Get selectioncheapest_eligiblebxgy.getSelection
Reward on the get units50% offbxgy.getDiscountPct
Free units are additionalfalsebxgy.freeUnitsAreAdditional
Target scopealltarget.scope
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
Er wordt om bevestiging gevraagd
  • Confirm which products this applies to. We could not read product targeting from the script.
De winkelwagen die het bewijst

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.

De winkelwagen die deze aanbieding activeert, en wat die kost
ArtikelAantalStukprijs
Softcover Notebook Set1$18.00
Braided USB-C Cable1$14.00
Subtotaal$32.00
Korting-$7.00
Totaal$25.00

Uitgelogd, of ingelogd als klant zonder tags.

Het bedrag waaraan de tool je toetst

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.

De winkelwagen die niets mag opleveren
  • swap the cart for a single Braided USB-C Cable
De winkelwagen die deze aanbieding niet mag activeren
ArtikelAantalStukprijs
Braided USB-C Cable1$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)

Herbouwd

Opnieuw gebouwd als bxgy-aanbieding.

Korting in de eigen winkelwagen: $7.00

Regels die verschillen
if (Input.cart.line_items.size > 1)
  line_item = Input.cart.line_items.sort_by { |line_item| line_item.variant.price }.first
Percentage off the cheapest item lezen

Cheapest item percentage unit count (D15)

Herbouwd

Opnieuw gebouwd als bxgy-aanbieding.

Korting in de eigen winkelwagen: $7.00

Regels die verschillen
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
Percentage off the cheapest item lezen

Veelgestelde 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

Buy one get one free

A set of paid units earns an extra unit at no charge.

Buy X get Y, inclusive

The same buy-and-get offer, counted so the reward sits inside each set rather than beside it.

Limit the discounted units

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.

We gebruiken essentiële cookies om deze site te laten werken en, alleen met uw toestemming, analytische cookies om verkeer te begrijpen. Lees ons Cookiebeleid.