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.
RicostruitoIt found the cheapest line in the cart and took a percentage off one unit of it.
I merchant lo hanno scritto in 2 modi diversi, e non significano tutti la stessa cosa. Ogni variante è riportata sotto con il proprio carrello e i propri numeri.
Incollalo in Recupero Script e guarda cosa ricostruisce
- Copia il Ruby con il pulsante accanto a ogni Script di questa pagina.
- Nell'admin di Shopify apri Stackable, poi Strumenti, poi Recupero Script.
- Incollalo lì, leggi cosa dichiara di costruire lo strumento e confrontalo con le impostazioni di questa pagina.
Recupero Script è nel piano Pro. Non viene pubblicato nulla finché non dichiari quanto scontava il vecchio Script e le due cifre non coincidono al centesimo.
Cheapest item percentage line count (D15)
RicostruitoCoppia rischiosa D15. Questo file e il suo gemello sembrano quasi identici e si comportano in modo diverso, quindi leggili entrambi prima di decidere quale hai.
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.cartRiconosciuto come Cheapest item discounted e ricostruito come offerta di tipo bxgy. Queste sono le vere chiavi di configurazione con cui l'offerta viene salvata, così puoi confrontare la tua ricostruzione campo per campo.
| Impostazione | Valore | Chiave di configurazione |
|---|---|---|
| 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.
Creato sul nostro negozio demo pubblico, Meridian Goods, così ogni prezzo qui si può riprodurre invece di doverci credere sulla parola. Lo sconto qui sotto è quello che lo stesso motore che gira al checkout calcola per questo esatto carrello.
| Articolo | Qtà | Prezzo unit. |
|---|---|---|
| Softcover Notebook Set | 1 | $18.00 |
| Braided USB-C Cable | 1 | $14.00 |
| Subtotale | $32.00 | |
| Sconto | -$7.00 | |
| Totale | $25.00 | |
Nessun accesso, oppure accesso come cliente senza tag.
Prima della pubblicazione, lo strumento chiede cosa faceva il tuo vecchio Script a un carrello che hai già. Impostalo su 2 di Braided USB-C Cable a $14.00 e la risposta è fissata.
Lo sconto è $7.00. Il totale pagato dal cliente è $21.00.
Il confronto è esatto, fino alla più piccola unità della tua valuta. Un centesimo di scarto e la pubblicazione resta bloccata, che è tutto il senso di questo passaggio.
- swap the cart for a single Braided USB-C Cable
| Articolo | Qtà | Prezzo unit. |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Subtotale | $14.00 | |
| Totale | $14.00 | |
Nessun accesso, oppure accesso come cliente senza tag.
Cheapest item percentage unit count (D15)
RicostruitoCoppia rischiosa D15. Questo file e il suo gemello sembrano quasi identici e si comportano in modo diverso, quindi leggili entrambi prima di decidere quale hai.
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.cartRiconosciuto come Cheapest item discounted e ricostruito come offerta di tipo bxgy. Queste sono le vere chiavi di configurazione con cui l'offerta viene salvata, così puoi confrontare la tua ricostruzione campo per campo.
| Impostazione | Valore | Chiave di configurazione |
|---|---|---|
| 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.
Creato sul nostro negozio demo pubblico, Meridian Goods, così ogni prezzo qui si può riprodurre invece di doverci credere sulla parola. Lo sconto qui sotto è quello che lo stesso motore che gira al checkout calcola per questo esatto carrello.
| Articolo | Qtà | Prezzo unit. |
|---|---|---|
| Softcover Notebook Set | 1 | $18.00 |
| Braided USB-C Cable | 1 | $14.00 |
| Subtotale | $32.00 | |
| Sconto | -$7.00 | |
| Totale | $25.00 | |
Nessun accesso, oppure accesso come cliente senza tag.
Prima della pubblicazione, lo strumento chiede cosa faceva il tuo vecchio Script a un carrello che hai già. Impostalo su 2 di Braided USB-C Cable a $14.00 e la risposta è fissata.
Lo sconto è $7.00. Il totale pagato dal cliente è $21.00.
Il confronto è esatto, fino alla più piccola unità della tua valuta. Un centesimo di scarto e la pubblicazione resta bloccata, che è tutto il senso di questo passaggio.
- swap the cart for a single Braided USB-C Cable
| Articolo | Qtà | Prezzo unit. |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Subtotale | $14.00 | |
| Totale | $14.00 | |
Nessun accesso, oppure accesso come cliente senza tag.
Due file che sembrano uguali
Ogni coppia qui sotto è la stessa forma di Script scritta in due modi. Cambiano una o due righe e fanno cose diverse, quindi leggile entrambe prima di decidere quale hai.
Coppia insidiosa D15
Cheapest item percentage line count (D15)
RicostruitoRicostruito come offerta bxgy.
Sconto sul suo carrello: $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)
RicostruitoRicostruito come offerta bxgy.
Sconto sul suo carrello: $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 }.firstDomande frequenti
- 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.
Forme di Script correlate
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.
Incolla il tuo Script e guardalo ricostruito
La ricostruzione viene confrontata con l'importo che il tuo vecchio Script applicava, prima che tu possa pubblicarla.