Naar de inhoud
Script-voorbeelden

Migrating a Shopify Script Creator script

Merchants who could not write Ruby used a generator, so the most common paste in the world is not hand-written at all. The 200 lines of class definitions are boilerplate; the array at the bottom is the offer.

Herbouwd
Wat het Script deed

It ran each campaign in the CAMPAIGNS array against the cart, in order, using a fixed library of qualifier and discount classes.

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.

Het Script

Herbouwd
De oorspronkelijke Ruby

# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
# Version 0.12.0

class Campaign
  def initialize(condition, *qualifiers)
    @condition = (condition.to_s + '?').to_sym
    @qualifiers = PostCartAmountQualifier ? [] : [] rescue qualifiers.compact
    @line_item_selector = qualifiers.last unless @line_item_selector
    qualifiers.compact.each do |qualifier|
      is_multi_select = qualifier.instance_variable_get(:@conditions).is_a?(Array)
      if is_multi_select
        qualifier.instance_variable_get(:@conditions).each do |nested_q|
          @post_amount_qualifier = nested_q if nested_q.is_a?(PostCartAmountQualifier)
          @qualifiers << qualifier
        end
      else
        @post_amount_qualifier = qualifier if qualifier.is_a?(PostCartAmountQualifier)
        @qualifiers << qualifier
      end
    end if @qualifiers.empty?
  end

  def qualifies?(cart)
    return true if @qualifiers.empty?
    @qualifiers.send(@condition) do |qualifier|
      is_selector = false
      if qualifier.is_a?(Selector) || qualifier.is_a?(Array)
        is_selector = true
      end rescue nil
      if is_selector
        raise 'Missing line item match type' if @li_match_type.nil?
        cart.line_items.send(@li_match_type) { |item| qualifier.match?(item) }
      else
        qualifier.match?(cart, @selector)
      end
    end
  end
end

class ProductTagSelector < Selector
  def initialize(match_type, match_condition, tags)
    @match_condition = match_condition
    @invert = match_type == :does_not
    @tags = tags.map(&:downcase)
  end

  def match?(line_item)
    product_tags = line_item.variant.product.tags.to_a.map(&:downcase)
    case @match_condition
      when :match
        return @invert ^ ((@tags & product_tags).length > 0)
      else
        return @invert ^ partial_match(@match_condition, product_tags, @tags)
    end
  end
end

class PercentageDiscount
  def initialize(percent, message)
    @percent = Decimal.new(percent) / 100.0
    @message = message
  end

  def apply(line_item)
    line_discount = line_item.line_price * @percent
    new_line_price = line_item.line_price - line_discount
    line_item.change_line_price(new_line_price, message: @message)
  end
end

CAMPAIGNS = [
  ConditionalDiscount.new(
    :all,
    nil,
    nil,
    ProductTagSelector.new(:does, :match, ["sale"]),
    PercentageDiscount.new(20, "20% off sale items"),
    0
  ),
].freeze

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart)
end

Output.cart = Input.cart
Wat het wordt

Herkend als Percentage off en herbouwd als aanbieding van het type volume. 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 typevolumetype
Countingper_productcounting
Recurrenceoncerecurrence
Tier 1min qty 1 -> 20% offtiers[0]
Target scopetagstarget.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.
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.

De winkelwagen die deze aanbieding activeert, en wat die kost
ArtikelAantalStukprijs
Ceramic Travel Mug1$22.00
Subtotaal$22.00
Korting-$4.40
Totaal$17.60

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 Ceramic Travel Mug voor $22.00 en het antwoord ligt vast.

De korting is $4.40. Het totaal dat de klant betaalde is $17.60.

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.

Veelgestelde vragen

Will Stackable rebuild this script?
Yes. Every spelling on this page is rebuilt today as a volume offer, and the cart above is the one that proves it.
What cart proves it works?
1 x Ceramic Travel Mug at $22.00. The engine takes $4.40 off, and checkout charges $17.60.
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.

Verwante Script-vormen

Help Center script template

The dialect Shopify’s own documentation taught, with its settings block above a do-not-edit banner.

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.