Naar de inhoud
Script-voorbeelden

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.

Herbouwd
Wat het Script deed

It discounted only the first few matching units and left the rest at full price.

Merchants hebben dit op 3 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.

Quantity limit first n discounted (D5)

Herbouwd

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

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

Herkend als Discount on the first few units 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_variantcounting
Recurrenceoncerecurrence
Tier 1min qty 1 -> free itemtiers[0]
Buy quantity1bxgy.buyQty
Get quantity2bxgy.getQty
Get selectionsame_itembxgy.getSelection
Reward on the get units30% offbxgy.getDiscountPct
Free units are additionalfalsebxgy.freeUnitsAreAdditional
Target scopeproduct_typestarget.scope
Target includesSaletarget.includeIds
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
Er wordt om bevestiging gevraagd
  • Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
Je krijgt te horen
  • The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
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.

  • 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.
De winkelwagen die deze aanbieding activeert, en wat die kost
ArtikelAantalStukprijs
Aria Wireless Earbuds1$79.00
Subtotaal$79.00
Korting-$23.70
Totaal$55.30

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 1 van Aria Wireless Earbuds voor $79.00 en het antwoord ligt vast.

De korting is $23.70. Het totaal dat de klant betaalde is $55.30.

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.

Quantity limit cheapest n (D5)

Herbouwd

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

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

Herkend als Discount on the first few units 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 1 -> free itemtiers[0]
Buy quantity1bxgy.buyQty
Get quantity2bxgy.getQty
Get selectioncheapest_eligiblebxgy.getSelection
Reward on the get units30% offbxgy.getDiscountPct
Free units are additionalfalsebxgy.freeUnitsAreAdditional
Target scopeproduct_typestarget.scope
Target includesSaletarget.includeIds
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
Er wordt om bevestiging gevraagd
  • Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
Je krijgt te horen
  • The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
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.

  • 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.
De winkelwagen die deze aanbieding activeert, en wat die kost
ArtikelAantalStukprijs
Impact Phone Case1$24.00
Aria Wireless Earbuds1$79.00
Subtotaal$103.00
Korting-$30.90
Totaal$72.10

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 1 van Impact Phone Case voor $24.00 en het antwoord ligt vast.

De korting is $7.20. Het totaal dat de klant betaalde is $16.80.

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.

Quantity limit first n discounted (D6)

Herbouwd

Riskant paar D6. 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_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.cart
Wat het wordt

Herkend als Discount on the first few units 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_variantcounting
Recurrenceoncerecurrence
Tier 1min qty 1 -> free itemtiers[0]
Buy quantity1bxgy.buyQty
Get quantity2bxgy.getQty
Get selectionsame_itembxgy.getSelection
Reward on the get units30% offbxgy.getDiscountPct
Free units are additionalfalsebxgy.freeUnitsAreAdditional
Target scopetagstarget.scope
Target includeslimitedtarget.includeIds
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
Er wordt om bevestiging gevraagd
  • Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
Je krijgt te horen
  • The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
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.

  • 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.
De winkelwagen die deze aanbieding activeert, en wat die kost
ArtikelAantalStukprijs
Aria Wireless Earbuds1$79.00
Subtotaal$79.00
Korting-$23.70
Totaal$55.30

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 1 van Aria Wireless Earbuds voor $79.00 en het antwoord ligt vast.

De korting is $23.70. Het totaal dat de klant betaalde is $55.30.

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 D5

Quantity limit first n discounted (D5)

Herbouwd

Opnieuw gebouwd als bxgy-aanbieding.

Korting in de eigen winkelwagen: $23.70

Regels die verschillen
    PercentageDiscount.new(30, "30% off first 2"),
Limit the discounted units lezen

Quantity limit cheapest n (D5)

Herbouwd

Opnieuw gebouwd als bxgy-aanbieding.

Korting in de eigen winkelwagen: $30.90

Regels die verschillen
    applicable_items = applicable_items.sort_by { |item| item.variant.price }
    PercentageDiscount.new(30, "30% off your 2 cheapest"),
Limit the discounted units lezen

Gevaarlijk paar D6

Quantity limit first n discounted (D6)

Herbouwd

Opnieuw gebouwd als bxgy-aanbieding.

Korting in de eigen winkelwagen: $23.70

Regels die verschillen
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")

En nog 9 afwijkende regels, op de eigen pagina.

Limit the discounted units lezen

Quantity limit purchase cap (D6)

Andere Shopify-API

Niet opnieuw gebouwd. This needs a different kind of Shopify app

Regels die verschillen
# 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 = limit

En nog 26 afwijkende regels, op de eigen pagina.

Purchase quantity limit 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 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.

Verwante Script-vormen

Purchase quantity limit

A cap on how many units a shopper could buy, enforced by trimming the cart.

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.

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.