Vai al contenuto
Esempi di Script

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.

Ricostruito
Cosa faceva lo Script

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

I merchant lo hanno scritto in 3 modi diversi, e non significano tutti la stessa cosa. Ogni variante è riportata sotto con il proprio carrello e i propri numeri.

Provalo sul tuo negozio

Incollalo in Recupero Script e guarda cosa ricostruisce

  1. Copia il Ruby con il pulsante accanto a ogni Script di questa pagina.
  2. Nell'admin di Shopify apri Stackable, poi Strumenti, poi Recupero Script.
  3. 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.

Quantity limit first n discounted (D5)

Ricostruito

Coppia rischiosa D5. Questo file e il suo gemello sembrano quasi identici e si comportano in modo diverso, quindi leggili entrambi prima di decidere quale hai.

Il Ruby originale

# 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
In cosa si trasforma

Riconosciuto come Discount on the first few units 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.

Ogni impostazione, nel dettaglio
ImpostazioneValoreChiave di configurazione
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
Ti verrà chiesta una conferma
  • Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
Ti verrà comunicato
  • The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
Il carrello che lo dimostra

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.

  • 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.
Il carrello che attiva questa offerta, e quanto costa
ArticoloQtàPrezzo unit.
Aria Wireless Earbuds1$79.00
Subtotale$79.00
Sconto-$23.70
Totale$55.30

Nessun accesso, oppure accesso come cliente senza tag.

La cifra con cui lo strumento ti confronta

Prima della pubblicazione, lo strumento chiede cosa faceva il tuo vecchio Script a un carrello che hai già. Impostalo su 1 di Aria Wireless Earbuds a $79.00 e la risposta è fissata.

Lo sconto è $23.70. Il totale pagato dal cliente è $55.30.

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.

Il carrello che non deve produrre nulla
  • swap the cart for a single Braided USB-C Cable
Il carrello che non deve attivare questa offerta
ArticoloQtàPrezzo unit.
Braided USB-C Cable1$14.00
Subtotale$14.00
Totale$14.00

Nessun accesso, oppure accesso come cliente senza tag.

Quantity limit cheapest n (D5)

Ricostruito

Coppia rischiosa D5. Questo file e il suo gemello sembrano quasi identici e si comportano in modo diverso, quindi leggili entrambi prima di decidere quale hai.

Il Ruby originale

# 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
In cosa si trasforma

Riconosciuto come Discount on the first few units 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.

Ogni impostazione, nel dettaglio
ImpostazioneValoreChiave di configurazione
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
Ti verrà chiesta una conferma
  • Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
Ti verrà comunicato
  • The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
Il carrello che lo dimostra

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.

  • 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.
Il carrello che attiva questa offerta, e quanto costa
ArticoloQtàPrezzo unit.
Impact Phone Case1$24.00
Aria Wireless Earbuds1$79.00
Subtotale$103.00
Sconto-$30.90
Totale$72.10

Nessun accesso, oppure accesso come cliente senza tag.

La cifra con cui lo strumento ti confronta

Prima della pubblicazione, lo strumento chiede cosa faceva il tuo vecchio Script a un carrello che hai già. Impostalo su 1 di Impact Phone Case a $24.00 e la risposta è fissata.

Lo sconto è $7.20. Il totale pagato dal cliente è $16.80.

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.

Il carrello che non deve produrre nulla
  • swap the cart for a single Braided USB-C Cable
Il carrello che non deve attivare questa offerta
ArticoloQtàPrezzo unit.
Braided USB-C Cable1$14.00
Subtotale$14.00
Totale$14.00

Nessun accesso, oppure accesso come cliente senza tag.

Quantity limit first n discounted (D6)

Ricostruito

Coppia rischiosa D6. Questo file e il suo gemello sembrano quasi identici e si comportano in modo diverso, quindi leggili entrambi prima di decidere quale hai.

Il Ruby originale

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
In cosa si trasforma

Riconosciuto come Discount on the first few units 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.

Ogni impostazione, nel dettaglio
ImpostazioneValoreChiave di configurazione
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
Ti verrà chiesta una conferma
  • Your Script limited this to a tag, vendor, or product type. We carried that filter over, so confirm it selects the products you expect.
Ti verrà comunicato
  • The unit limit counts per matching group, not per cart line, so a cart with two separate lines can reach the limit twice.
Il carrello che lo dimostra

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.

  • 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.
Il carrello che attiva questa offerta, e quanto costa
ArticoloQtàPrezzo unit.
Aria Wireless Earbuds1$79.00
Subtotale$79.00
Sconto-$23.70
Totale$55.30

Nessun accesso, oppure accesso come cliente senza tag.

La cifra con cui lo strumento ti confronta

Prima della pubblicazione, lo strumento chiede cosa faceva il tuo vecchio Script a un carrello che hai già. Impostalo su 1 di Aria Wireless Earbuds a $79.00 e la risposta è fissata.

Lo sconto è $23.70. Il totale pagato dal cliente è $55.30.

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.

Il carrello che non deve produrre nulla
  • swap the cart for a single Braided USB-C Cable
Il carrello che non deve attivare questa offerta
ArticoloQtàPrezzo unit.
Braided USB-C Cable1$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 D5

Quantity limit first n discounted (D5)

Ricostruito

Ricostruito come offerta bxgy.

Sconto sul suo carrello: $23.70

Righe che cambiano
    PercentageDiscount.new(30, "30% off first 2"),
Leggi Limit the discounted units

Quantity limit cheapest n (D5)

Ricostruito

Ricostruito come offerta bxgy.

Sconto sul suo carrello: $30.90

Righe che cambiano
    applicable_items = applicable_items.sort_by { |item| item.variant.price }
    PercentageDiscount.new(30, "30% off your 2 cheapest"),
Leggi Limit the discounted units

Coppia insidiosa D6

Quantity limit first n discounted (D6)

Ricostruito

Ricostruito come offerta bxgy.

Sconto sul suo carrello: $23.70

Righe che cambiano
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")

E altre 9 righe diverse, sulla sua pagina.

Leggi Limit the discounted units

Quantity limit purchase cap (D6)

Altra API Shopify

Non ricostruito. This needs a different kind of Shopify app

Righe che cambiano
# 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

E altre 26 righe diverse, sulla sua pagina.

Leggi Purchase quantity limit

Domande 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 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.

Forme di Script correlate

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.

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.

Utilizziamo cookie essenziali per il funzionamento del sito e, solo con il Suo consenso, cookie di analisi per comprendere il traffico. Legga la nostra Informativa sui Cookie.