Migrating a script that limited how many units were discounted
The cap is not the trap. The trap is a single sort line: with it the CHEAPEST units are discounted, without it the FIRST ones are, and both files are dense with the same split call that also makes them look like a BOGO.
GenopbyggetIt discounted only the first few matching units and left the rest at full price.
Handlende har skrevet det her på 3 forskellige måder, og de betyder ikke alle det samme. Hver variant står nedenfor med sin egen kurv og sine egne tal.
Indsæt det i Scripts-redning, og se hvad der bliver bygget
- Kopiér Ruby-koden med knappen ved siden af ethvert Script på siden.
- Åbn Stackable i Shopify-administrationen, derefter Værktøjer og så Scripts-redning.
- Indsæt det der, læs hvad værktøjet siger, det vil bygge, og hold det op mod indstillingerne på denne side.
Scripts-redning hører til Pro-abonnementet. Intet bliver udgivet, før du oplyser, hvad dit gamle Script gav i rabat, og de to beløb stemmer helt ned på øret.
Quantity limit first n discounted (D5)
GenopbyggetFarligt par D5. Denne fil og dens makker ser næsten ens ud og opfører sig forskelligt, så læs dem begge, før du afgør, hvilken du har.
# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
class ConditionalDiscount < Campaign
def initialize(condition, customer_qualifier, cart_qualifier, line_item_selector, discount, max_discounts)
super(condition, customer_qualifier, cart_qualifier, line_item_selector)
@discount = discount
@items_to_discount = max_discounts == 0 ? nil : max_discounts
end
def run(cart)
return unless qualifies?(cart)
applicable_items = cart.line_items.select { |item| @line_item_selector.nil? || @line_item_selector.match?(item) }
applicable_items.each do |item|
break if @items_to_discount == 0
if !@items_to_discount.nil? && (item.quantity > @items_to_discount)
new_item = item.split({ take: @items_to_discount })
@discount.apply(new_item)
position = cart.line_items.find_index(item)
cart.line_items.insert(position + 1, new_item)
@items_to_discount = 0
else
@discount.apply(item)
@items_to_discount -= item.quantity unless @items_to_discount.nil?
end
end
end
end
CAMPAIGNS = [
ConditionalDiscount.new(
:all, nil, nil,
ProductTypeSelector.new(:does, ["Sale"]),
PercentageDiscount.new(30, "30% off first 2"),
2
),
].freeze
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cartGenkendt som Discount on the first few units og genopbygget som et tilbud af typen bxgy. Det her er de rigtige konfigurationsnøgler, som tilbuddet gemmes med, så du kan tjekke din egen genopbygning felt for felt.
| Indstilling | Værdi | Konfigurationsnøgle |
|---|---|---|
| Offer type | bxgy | type |
| Counting | per_variant | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 1 -> free item | tiers[0] |
| Buy quantity | 1 | bxgy.buyQty |
| Get quantity | 2 | bxgy.getQty |
| Get selection | same_item | bxgy.getSelection |
| Reward on the get units | 30% off | bxgy.getDiscountPct |
| Free units are additional | false | bxgy.freeUnitsAreAdditional |
| Target scope | product_types | target.scope |
| Target includes | Sale | target.includeIds |
| Method | automatic | discountMethod |
| Allocation | standard | stacking.allocation |
| Combines with other discounts | no | stacking |
- Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
- The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
Bygget i vores offentlige demobutik, Meridian Goods, så hver pris her kan efterregnes i stedet for at blive taget på tro. Rabatten nedenfor er det, som den samme motor, der kører i kassen, beregner for præcis denne kurv.
- No Meridian Goods product carries the type Sale. Set it on Aria Wireless Earbuds in Shopify before you build the cart, and on nothing else.
| Vare | Antal | Stykpris |
|---|---|---|
| Aria Wireless Earbuds | 1 | $79.00 |
| Subtotal | $79.00 | |
| Rabat | -$23.70 | |
| Total | $55.30 | |
Logget ud, eller logget ind som kunde uden tags.
Inden du udgiver, spørger værktøjet, hvad dit gamle Script gjorde ved en kurv, du allerede har. Sæt den til 1 af Aria Wireless Earbuds à $79.00, så ligger svaret fast.
Rabatten er $23.70. Det beløb, kunden betalte, er $55.30.
Sammenligningen er præcis, helt ned til den mindste enhed i din valuta. En øre ved siden af, og udgivelsen forbliver spærret, hvilket er hele pointen med trinnet.
- swap the cart for a single Braided USB-C Cable
| Vare | Antal | Stykpris |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Subtotal | $14.00 | |
| Total | $14.00 | |
Logget ud, eller logget ind som kunde uden tags.
Quantity limit cheapest n (D5)
GenopbyggetFarligt par D5. Denne fil og dens makker ser næsten ens ud og opfører sig forskelligt, så læs dem begge, før du afgør, hvilken du har.
# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
class ConditionalDiscount < Campaign
def initialize(condition, customer_qualifier, cart_qualifier, line_item_selector, discount, max_discounts)
super(condition, customer_qualifier, cart_qualifier, line_item_selector)
@discount = discount
@items_to_discount = max_discounts == 0 ? nil : max_discounts
end
def run(cart)
return unless qualifies?(cart)
applicable_items = cart.line_items.select { |item| @line_item_selector.nil? || @line_item_selector.match?(item) }
applicable_items = applicable_items.sort_by { |item| item.variant.price }
applicable_items.each do |item|
break if @items_to_discount == 0
if !@items_to_discount.nil? && (item.quantity > @items_to_discount)
new_item = item.split({ take: @items_to_discount })
@discount.apply(new_item)
position = cart.line_items.find_index(item)
cart.line_items.insert(position + 1, new_item)
@items_to_discount = 0
else
@discount.apply(item)
@items_to_discount -= item.quantity unless @items_to_discount.nil?
end
end
end
end
CAMPAIGNS = [
ConditionalDiscount.new(
:all, nil, nil,
ProductTypeSelector.new(:does, ["Sale"]),
PercentageDiscount.new(30, "30% off your 2 cheapest"),
2
),
].freeze
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cartGenkendt som Discount on the first few units og genopbygget som et tilbud af typen bxgy. Det her er de rigtige konfigurationsnøgler, som tilbuddet gemmes med, så du kan tjekke din egen genopbygning felt for felt.
| Indstilling | Værdi | Konfigurationsnøgle |
|---|---|---|
| Offer type | bxgy | type |
| Counting | per_group | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 1 -> free item | tiers[0] |
| Buy quantity | 1 | bxgy.buyQty |
| Get quantity | 2 | bxgy.getQty |
| Get selection | cheapest_eligible | bxgy.getSelection |
| Reward on the get units | 30% off | bxgy.getDiscountPct |
| Free units are additional | false | bxgy.freeUnitsAreAdditional |
| Target scope | product_types | target.scope |
| Target includes | Sale | target.includeIds |
| Method | automatic | discountMethod |
| Allocation | standard | stacking.allocation |
| Combines with other discounts | no | stacking |
- Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
- The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
Bygget i vores offentlige demobutik, Meridian Goods, så hver pris her kan efterregnes i stedet for at blive taget på tro. Rabatten nedenfor er det, som den samme motor, der kører i kassen, beregner for præcis denne kurv.
- No Meridian Goods product carries the type Sale. Set it on Aria Wireless Earbuds and Impact Phone Case in Shopify before you build the cart, and on nothing else.
| Vare | Antal | Stykpris |
|---|---|---|
| Impact Phone Case | 1 | $24.00 |
| Aria Wireless Earbuds | 1 | $79.00 |
| Subtotal | $103.00 | |
| Rabat | -$30.90 | |
| Total | $72.10 | |
Logget ud, eller logget ind som kunde uden tags.
Inden du udgiver, spørger værktøjet, hvad dit gamle Script gjorde ved en kurv, du allerede har. Sæt den til 1 af Impact Phone Case à $24.00, så ligger svaret fast.
Rabatten er $7.20. Det beløb, kunden betalte, er $16.80.
Sammenligningen er præcis, helt ned til den mindste enhed i din valuta. En øre ved siden af, og udgivelsen forbliver spærret, hvilket er hele pointen med trinnet.
- swap the cart for a single Braided USB-C Cable
| Vare | Antal | Stykpris |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Subtotal | $14.00 | |
| Total | $14.00 | |
Logget ud, eller logget ind som kunde uden tags.
Quantity limit first n discounted (D6)
GenopbyggetFarligt par D6. Denne fil og dens makker ser næsten ens ud og opfører sig forskelligt, så læs dem begge, før du afgør, hvilken du har.
DISCOUNT_PERCENTAGE = 30
MAX_UNITS = 2
num_to_discount = MAX_UNITS
Input.cart.line_items.each_with_index do |line_item, position|
break if num_to_discount <= 0
next unless line_item.variant.product.tags.include?("limited")
if line_item.quantity > num_to_discount
split_line_item = line_item.split(take: num_to_discount)
Input.cart.line_items.insert(position + 1, split_line_item)
line_item = split_line_item
end
line_item.change_line_price(
line_item.line_price * (1.0 - (DISCOUNT_PERCENTAGE / 100.0)),
message: "30% off your first 2"
)
num_to_discount -= line_item.quantity
end
Output.cart = Input.cartGenkendt som Discount on the first few units og genopbygget som et tilbud af typen bxgy. Det her er de rigtige konfigurationsnøgler, som tilbuddet gemmes med, så du kan tjekke din egen genopbygning felt for felt.
| Indstilling | Værdi | Konfigurationsnøgle |
|---|---|---|
| Offer type | bxgy | type |
| Counting | per_variant | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 1 -> free item | tiers[0] |
| Buy quantity | 1 | bxgy.buyQty |
| Get quantity | 2 | bxgy.getQty |
| Get selection | same_item | bxgy.getSelection |
| Reward on the get units | 30% off | bxgy.getDiscountPct |
| Free units are additional | false | bxgy.freeUnitsAreAdditional |
| Target scope | tags | target.scope |
| Target includes | limited | target.includeIds |
| Method | automatic | discountMethod |
| Allocation | standard | stacking.allocation |
| Combines with other discounts | no | stacking |
- Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
- The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
Bygget i vores offentlige demobutik, Meridian Goods, så hver pris her kan efterregnes i stedet for at blive taget på tro. Rabatten nedenfor er det, som den samme motor, der kører i kassen, beregner for præcis denne kurv.
- No Meridian Goods product carries the tag limited. Set it on Aria Wireless Earbuds in Shopify before you build the cart, and on nothing else.
| Vare | Antal | Stykpris |
|---|---|---|
| Aria Wireless Earbuds | 1 | $79.00 |
| Subtotal | $79.00 | |
| Rabat | -$23.70 | |
| Total | $55.30 | |
Logget ud, eller logget ind som kunde uden tags.
Inden du udgiver, spørger værktøjet, hvad dit gamle Script gjorde ved en kurv, du allerede har. Sæt den til 1 af Aria Wireless Earbuds à $79.00, så ligger svaret fast.
Rabatten er $23.70. Det beløb, kunden betalte, er $55.30.
Sammenligningen er præcis, helt ned til den mindste enhed i din valuta. En øre ved siden af, og udgivelsen forbliver spærret, hvilket er hele pointen med trinnet.
- swap the cart for a single Braided USB-C Cable
| Vare | Antal | Stykpris |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Subtotal | $14.00 | |
| Total | $14.00 | |
Logget ud, eller logget ind som kunde uden tags.
To filer, der ligner hinanden
Hvert par herunder er den samme Script-form skrevet på to måder. De adskiller sig på en eller to linjer og gør noget forskelligt, så læs dem begge, før du afgør, hvilken du har.
Farligt par D5
Quantity limit first n discounted (D5)
GenopbyggetBygges om til et bxgy-tilbud.
Rabat i dens egen kurv: $23.70
PercentageDiscount.new(30, "30% off first 2"),Quantity limit cheapest n (D5)
GenopbyggetBygges om til et bxgy-tilbud.
Rabat i dens egen kurv: $30.90
applicable_items = applicable_items.sort_by { |item| item.variant.price }
PercentageDiscount.new(30, "30% off your 2 cheapest"),Farligt par D6
Quantity limit first n discounted (D6)
GenopbyggetBygges om til et bxgy-tilbud.
Rabat i dens egen kurv: $23.70
DISCOUNT_PERCENTAGE = 30
MAX_UNITS = 2
num_to_discount = MAX_UNITS
Input.cart.line_items.each_with_index do |line_item, position|
break if num_to_discount <= 0
next unless line_item.variant.product.tags.include?("limited")Og 9 flere forskellige linjer, på dens egen side.
Quantity limit purchase cap (D6)
Andet Shopify-APIBygges ikke om. This needs a different kind of Shopify app
# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
class QuantityLimit < Campaign
def initialize(condition, customer_qualifier, cart_qualifier, line_item_selector, limit_by, limit)
super(condition, customer_qualifier, cart_qualifier, line_item_selector)
@limit_by = limit_by
@limit = limitOg 26 flere forskellige linjer, på dens egen side.
Ofte stillede spørgsmål
- 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 Aria Wireless Earbuds at $79.00. The engine takes $23.70 off, and checkout charges $55.30.
- What will I have to check before publishing?
- Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect. The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
Beslægtede Script-typer
A cap on how many units a shopper could buy, enforced by trimming the cart.
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.
Indsæt dit Script og se det blive genopbygget
Genopbygningen holdes op mod det beløb, dit gamle Script opkrævede, før du kan udgive den.