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.
NachgebautIt found the cheapest line in the cart and took a percentage off one unit of it.
Händler haben das auf 2 verschiedene Arten geschrieben, und sie bedeuten nicht alle dasselbe. Jede Schreibweise steht unten mit eigenem Warenkorb und eigenen Zahlen.
Fügen Sie es in die Scripts-Rettung ein und sehen Sie, was daraus wird
- Kopieren Sie das Ruby mit der Schaltfläche neben jedem Script auf dieser Seite.
- Öffnen Sie im Shopify-Adminbereich Stackable, dann Tools, dann Scripts-Rettung.
- Fügen Sie es dort ein, lesen Sie, was das Tool bauen will, und vergleichen Sie das mit den Einstellungen auf dieser Seite.
Die Scripts-Rettung gehört zum Pro-Tarif. Nichts wird veröffentlicht, bevor Sie angeben, was Ihr altes Script berechnet hat, und beide Zahlen auf den Cent genau übereinstimmen.
Cheapest item percentage line count (D15)
NachgebautGefährliches Paar D15. Diese Datei und ihr Gegenstück sehen fast gleich aus und verhalten sich unterschiedlich, lesen Sie also beide, bevor Sie entscheiden, welche Sie haben.
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.cartErkannt als Cheapest item discounted und neu aufgebaut als Angebot vom Typ bxgy. Das sind die echten Konfigurationsschlüssel, mit denen das Angebot gespeichert wird, sodass Sie Ihren eigenen Nachbau Feld für Feld damit abgleichen können.
| Einstellung | Wert | Konfigurationsschlüssel |
|---|---|---|
| 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.
Aufgebaut in unserem öffentlichen Demo-Shop Meridian Goods, damit sich jeder Preis hier nachrechnen lässt und nicht geglaubt werden muss. Der Rabatt unten ist das, was dieselbe Engine, die im Checkout läuft, für genau diesen Warenkorb berechnet.
| Artikel | Menge | Einzelpreis |
|---|---|---|
| Softcover Notebook Set | 1 | $18.00 |
| Braided USB-C Cable | 1 | $14.00 |
| Zwischensumme | $32.00 | |
| Rabatt | -$7.00 | |
| Summe | $25.00 | |
Nicht angemeldet oder als Kunde ohne Tags angemeldet.
Vor dem Veröffentlichen fragt das Tool, was Ihr altes Script mit einem Warenkorb gemacht hat, den Sie bereits haben. Stellen Sie ihn auf 2 von Braided USB-C Cable zu $14.00 ein, dann steht die Antwort fest.
Der Rabatt beträgt $7.00. Der Betrag, den der Käufer gezahlt hat, ist $21.00.
Der Vergleich ist exakt, bis auf die kleinste Einheit Ihrer Währung. Ein Cent daneben und das Veröffentlichen bleibt gesperrt, genau darum geht es bei diesem Schritt.
- swap the cart for a single Braided USB-C Cable
| Artikel | Menge | Einzelpreis |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Zwischensumme | $14.00 | |
| Summe | $14.00 | |
Nicht angemeldet oder als Kunde ohne Tags angemeldet.
Cheapest item percentage unit count (D15)
NachgebautGefährliches Paar D15. Diese Datei und ihr Gegenstück sehen fast gleich aus und verhalten sich unterschiedlich, lesen Sie also beide, bevor Sie entscheiden, welche Sie haben.
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.cartErkannt als Cheapest item discounted und neu aufgebaut als Angebot vom Typ bxgy. Das sind die echten Konfigurationsschlüssel, mit denen das Angebot gespeichert wird, sodass Sie Ihren eigenen Nachbau Feld für Feld damit abgleichen können.
| Einstellung | Wert | Konfigurationsschlüssel |
|---|---|---|
| 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.
Aufgebaut in unserem öffentlichen Demo-Shop Meridian Goods, damit sich jeder Preis hier nachrechnen lässt und nicht geglaubt werden muss. Der Rabatt unten ist das, was dieselbe Engine, die im Checkout läuft, für genau diesen Warenkorb berechnet.
| Artikel | Menge | Einzelpreis |
|---|---|---|
| Softcover Notebook Set | 1 | $18.00 |
| Braided USB-C Cable | 1 | $14.00 |
| Zwischensumme | $32.00 | |
| Rabatt | -$7.00 | |
| Summe | $25.00 | |
Nicht angemeldet oder als Kunde ohne Tags angemeldet.
Vor dem Veröffentlichen fragt das Tool, was Ihr altes Script mit einem Warenkorb gemacht hat, den Sie bereits haben. Stellen Sie ihn auf 2 von Braided USB-C Cable zu $14.00 ein, dann steht die Antwort fest.
Der Rabatt beträgt $7.00. Der Betrag, den der Käufer gezahlt hat, ist $21.00.
Der Vergleich ist exakt, bis auf die kleinste Einheit Ihrer Währung. Ein Cent daneben und das Veröffentlichen bleibt gesperrt, genau darum geht es bei diesem Schritt.
- swap the cart for a single Braided USB-C Cable
| Artikel | Menge | Einzelpreis |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Zwischensumme | $14.00 | |
| Summe | $14.00 | |
Nicht angemeldet oder als Kunde ohne Tags angemeldet.
Zwei Dateien, die gleich aussehen
Jedes Paar unten ist dieselbe Script-Form in zwei Schreibweisen. Sie unterscheiden sich in ein, zwei Zeilen und tun Unterschiedliches. Lesen Sie beide, bevor Sie entscheiden, welche Sie haben.
Gefährliches Paar D15
Cheapest item percentage line count (D15)
NachgebautWird als bxgy-Angebot nachgebaut.
Rabatt im eigenen Warenkorb: $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)
NachgebautWird als bxgy-Angebot nachgebaut.
Rabatt im eigenen Warenkorb: $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 }.firstHäufige Fragen
- 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.
Verwandte Script-Formen
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.
Fügen Sie Ihr Script ein und sehen Sie den Nachbau
Der Nachbau wird mit dem Betrag abgeglichen, den Ihr altes Script berechnet hat, bevor Sie ihn veröffentlichen können.