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.
GenopbyggetIt ran each campaign in the CAMPAIGNS array against the cart, in order, using a fixed library of qualifier and discount classes.
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.
Selve dette Script
Genopbygget# 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.cartGenkendt som Percentage off og genopbygget som et tilbud af typen volume. 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 | volume | type |
| Counting | per_product | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 1 -> 20% off | tiers[0] |
| Target scope | tags | 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.
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.
| Vare | Antal | Stykpris |
|---|---|---|
| Ceramic Travel Mug | 1 | $22.00 |
| Subtotal | $22.00 | |
| Rabat | -$4.40 | |
| Total | $17.60 | |
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 Ceramic Travel Mug à $22.00, så ligger svaret fast.
Rabatten er $4.40. Det beløb, kunden betalte, er $17.60.
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.
Ofte stillede spørgsmål
- 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.
Beslægtede Script-typer
The dialect Shopify’s own documentation taught, with its settings block above a do-not-edit banner.
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.