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.
OmbyggtIt found the cheapest line in the cart and took a percentage off one unit of it.
Handlare har skrivit det här på 2 olika sätt, och de betyder inte alla samma sak. Varje variant finns nedan med en egen varukorg och egna siffror.
Klistra in det i Scripts-räddning och se vad som byggs
- Kopiera Ruby-koden med knappen bredvid vilket Script som helst på sidan.
- Öppna Stackable i Shopify-adminen, sedan Verktyg, sedan Scripts-räddning.
- Klistra in det där, läs vad verktyget säger att det ska bygga och jämför med inställningarna på den här sidan.
Scripts-räddning ingår i Pro-planen. Ingenting publiceras förrän du anger vad ditt gamla Script gav i rabatt och båda beloppen stämmer på öret.
Cheapest item percentage line count (D15)
OmbyggtRiskabelt par D15. Den här filen och dess motpart ser nästan likadana ut och beter sig olika, så läs båda innan du avgör vilken du har.
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.cartIdentifierat som Cheapest item discounted och ombyggt till ett erbjudande av typen bxgy. Det här är de verkliga konfigurationsnycklarna som erbjudandet sparas med, så du kan stämma av din egen ombyggnad fält för fält.
| Inställning | Värde | Konfigurationsnyckel |
|---|---|---|
| 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.
Byggd i vår publika demobutik, Meridian Goods, så att varje pris här går att räkna efter i stället för att tas på förtroende. Rabatten nedan är vad samma motor som kör i kassan räknar fram för exakt den här varukorgen.
| Artikel | Antal | Styckpris |
|---|---|---|
| Softcover Notebook Set | 1 | $18.00 |
| Braided USB-C Cable | 1 | $14.00 |
| Delsumma | $32.00 | |
| Rabatt | -$7.00 | |
| Summa | $25.00 | |
Utloggad, eller inloggad som kund utan taggar.
Före publicering frågar verktyget vad ditt gamla Script gjorde med en varukorg du redan har. Sätt den till 2 av Braided USB-C Cable à $14.00, så är svaret givet.
Rabatten är $7.00. Summan som kunden betalade är $21.00.
Jämförelsen är exakt, ned till minsta enhet i din valuta. Ett öre fel och publiceringen förblir stängd, vilket är hela poängen med steget.
- swap the cart for a single Braided USB-C Cable
| Artikel | Antal | Styckpris |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Delsumma | $14.00 | |
| Summa | $14.00 | |
Utloggad, eller inloggad som kund utan taggar.
Cheapest item percentage unit count (D15)
OmbyggtRiskabelt par D15. Den här filen och dess motpart ser nästan likadana ut och beter sig olika, så läs båda innan du avgör vilken du har.
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.cartIdentifierat som Cheapest item discounted och ombyggt till ett erbjudande av typen bxgy. Det här är de verkliga konfigurationsnycklarna som erbjudandet sparas med, så du kan stämma av din egen ombyggnad fält för fält.
| Inställning | Värde | Konfigurationsnyckel |
|---|---|---|
| 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.
Byggd i vår publika demobutik, Meridian Goods, så att varje pris här går att räkna efter i stället för att tas på förtroende. Rabatten nedan är vad samma motor som kör i kassan räknar fram för exakt den här varukorgen.
| Artikel | Antal | Styckpris |
|---|---|---|
| Softcover Notebook Set | 1 | $18.00 |
| Braided USB-C Cable | 1 | $14.00 |
| Delsumma | $32.00 | |
| Rabatt | -$7.00 | |
| Summa | $25.00 | |
Utloggad, eller inloggad som kund utan taggar.
Före publicering frågar verktyget vad ditt gamla Script gjorde med en varukorg du redan har. Sätt den till 2 av Braided USB-C Cable à $14.00, så är svaret givet.
Rabatten är $7.00. Summan som kunden betalade är $21.00.
Jämförelsen är exakt, ned till minsta enhet i din valuta. Ett öre fel och publiceringen förblir stängd, vilket är hela poängen med steget.
- swap the cart for a single Braided USB-C Cable
| Artikel | Antal | Styckpris |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Delsumma | $14.00 | |
| Summa | $14.00 | |
Utloggad, eller inloggad som kund utan taggar.
Två filer som ser likadana ut
Varje par nedan är samma Script-form skriven på två sätt. De skiljer sig åt på en eller två rader och gör olika saker, så läs båda innan du avgör vilken du har.
Farligt par D15
Cheapest item percentage line count (D15)
OmbyggtByggs om till ett bxgy-erbjudande.
Rabatt i sin egen varukorg: $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)
OmbyggtByggs om till ett bxgy-erbjudande.
Rabatt i sin egen varukorg: $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 }.firstVanliga frågor
- 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.
Besläktade Script-former
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.
Klistra in ditt Script och se det byggas om
Ombyggnaden stäms av mot beloppet som ditt gamla Script tog ut innan du kan publicera den.