Migrating a script scoped to a tag, vendor or product type
A dropped filter is the most expensive class of mistake in a migration, because it is silent and it is confident: the percentage is read perfectly and applied to everything you sell. Note that Scripts could not read collections at all.
OmbyggtIt skipped every line whose product did not carry the listed tag, vendor or type.
Handlare har skrivit det här på 3 olika sätt, och de betyder inte alla samma sak. Varje variant finns nedan med en egen varukorg och egna siffror.
Klistra in det i Scripts-räddning och se vad som byggs
- Kopiera Ruby-koden med knappen bredvid vilket Script som helst på sidan.
- Öppna Stackable i Shopify-adminen, sedan Verktyg, sedan Scripts-räddning.
- Klistra in det där, läs vad verktyget säger att det ska bygga och jämför med inställningarna på den här sidan.
Scripts-räddning ingår i Pro-planen. Ingenting publiceras förrän du anger vad ditt gamla Script gav i rabatt och båda beloppen stämmer på öret.
Official dialect, product selector include (D13)
OmbyggtRiskabelt par D13. Den här filen och dess motpart ser nästan likadana ut och beter sig olika, så läs båda innan du avgör vilken du har.
# ================================ Customizable Settings ================================
PRODUCT_DISCOUNTS = [
{
product_selector_match_type: :include,
product_selector_type: :tag,
product_selectors: ["sale"],
discount_type: :percent,
discount_amount: 20,
discount_message: "20% off sale items",
},
]
# ================================ Script Code (do not edit) ================================
class ProductSelector
def initialize(match_type, selector_type, selectors)
@match_type = match_type
@comparator = match_type == :include ? 'any?' : 'none?'
@selector_type = selector_type
@selectors = selectors
end
def match?(line_item)
if self.respond_to?(@selector_type)
self.send(@selector_type, line_item)
else
raise RuntimeError.new('Invalid product selector type')
end
end
def tag(line_item)
product_tags = line_item.variant.product.tags.map { |tag| tag.downcase.strip }
@selectors = @selectors.map { |selector| selector.downcase.strip }
(@selectors & product_tags).send(@comparator)
end
def type(line_item)
@selectors = @selectors.map { |selector| selector.downcase.strip }
(@selectors.include?(line_item.variant.product.product_type.downcase.strip)) == (@match_type == :include)
end
def vendor(line_item)
@selectors = @selectors.map { |selector| selector.downcase.strip }
(@selectors.include?(line_item.variant.product.vendor.downcase.strip)) == (@match_type == :include)
end
def product_id(line_item)
(@selectors.include?(line_item.variant.product.id)) == (@match_type == :include)
end
def variant_id(line_item)
(@selectors.include?(line_item.variant.id)) == (@match_type == :include)
end
def all(line_item)
true
end
end
class ProductDiscountCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart)
@campaigns.each do |campaign|
product_selector = ProductSelector.new(
campaign[:product_selector_match_type],
campaign[:product_selector_type],
campaign[:product_selectors],
)
cart.line_items.each do |line_item|
next unless product_selector.match?(line_item)
line_item.change_line_price(
line_item.line_price * (1 - (campaign[:discount_amount] * 0.01)),
message: campaign[:discount_message],
)
end
end
end
end
CAMPAIGNS = [
ProductDiscountCampaign.new(PRODUCT_DISCOUNTS),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cartIdentifierat som Percentage off och ombyggt till ett erbjudande av typen volume. Det här är de verkliga konfigurationsnycklarna som erbjudandet sparas med, så du kan stämma av din egen ombyggnad fält för fält.
| Inställning | Värde | Konfigurationsnyckel |
|---|---|---|
| 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.
Byggd i vår publika demobutik, Meridian Goods, så att varje pris här går att räkna efter i stället för att tas på förtroende. Rabatten nedan är vad samma motor som kör i kassan räknar fram för exakt den här varukorgen.
| Artikel | Antal | Styckpris |
|---|---|---|
| Ceramic Travel Mug | 1 | $22.00 |
| Delsumma | $22.00 | |
| Rabatt | -$4.40 | |
| Summa | $17.60 | |
Utloggad, eller inloggad som kund utan taggar.
Före publicering frågar verktyget vad ditt gamla Script gjorde med en varukorg du redan har. Sätt den till 1 av Ceramic Travel Mug à $22.00, så är svaret givet.
Rabatten är $4.40. Summan som kunden betalade är $17.60.
Jämförelsen är exakt, ned till minsta enhet i din valuta. Ett öre fel och publiceringen förblir stängd, vilket är hela poängen med steget.
- swap the cart for a single Braided USB-C Cable
| Artikel | Antal | Styckpris |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Delsumma | $14.00 | |
| Summa | $14.00 | |
Utloggad, eller inloggad som kund utan taggar.
Official dialect, product selector exclude (D13)
OmbyggtRiskabelt par D13. Den här filen och dess motpart ser nästan likadana ut och beter sig olika, så läs båda innan du avgör vilken du har.
# ================================ Customizable Settings ================================
PRODUCT_DISCOUNTS = [
{
product_selector_match_type: :exclude,
product_selector_type: :tag,
product_selectors: ["sale"],
discount_type: :percent,
discount_amount: 20,
discount_message: "20% off full-price items",
},
]
# ================================ Script Code (do not edit) ================================
class ProductSelector
def initialize(match_type, selector_type, selectors)
@match_type = match_type
@comparator = match_type == :include ? 'any?' : 'none?'
@selector_type = selector_type
@selectors = selectors
end
def match?(line_item)
if self.respond_to?(@selector_type)
self.send(@selector_type, line_item)
else
raise RuntimeError.new('Invalid product selector type')
end
end
def tag(line_item)
product_tags = line_item.variant.product.tags.map { |tag| tag.downcase.strip }
@selectors = @selectors.map { |selector| selector.downcase.strip }
(@selectors & product_tags).send(@comparator)
end
def all(line_item)
true
end
end
class ProductDiscountCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart)
@campaigns.each do |campaign|
product_selector = ProductSelector.new(
campaign[:product_selector_match_type],
campaign[:product_selector_type],
campaign[:product_selectors],
)
cart.line_items.each do |line_item|
next unless product_selector.match?(line_item)
line_item.change_line_price(
line_item.line_price * (1 - (campaign[:discount_amount] * 0.01)),
message: campaign[:discount_message],
)
end
end
end
end
CAMPAIGNS = [
ProductDiscountCampaign.new(PRODUCT_DISCOUNTS),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cartIdentifierat som Percentage off och ombyggt till ett erbjudande av typen volume. Det här är de verkliga konfigurationsnycklarna som erbjudandet sparas med, så du kan stämma av din egen ombyggnad fält för fält.
| Inställning | Värde | Konfigurationsnyckel |
|---|---|---|
| Offer type | volume | type |
| Counting | per_product | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 1 -> 20% off | tiers[0] |
| Target scope | all | target.scope |
| Target excludes | sale | target.excludeIds |
| 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.
Byggd i vår publika demobutik, Meridian Goods, så att varje pris här går att räkna efter i stället för att tas på förtroende. Rabatten nedan är vad samma motor som kör i kassan räknar fram för exakt den här varukorgen.
- Targeting excludes sale. No Meridian Goods product carries those, so every product stays eligible and the exclusion is proved by the campaign settings, not by the cart.
| Artikel | Antal | Styckpris |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Delsumma | $14.00 | |
| Rabatt | -$2.80 | |
| Summa | $11.20 | |
Utloggad, eller inloggad som kund utan taggar.
Före publicering frågar verktyget vad ditt gamla Script gjorde med en varukorg du redan har. Sätt den till 1 av Braided USB-C Cable à $14.00, så är svaret givet.
Rabatten är $2.80. Summan som kunden betalade är $11.20.
Jämförelsen är exakt, ned till minsta enhet i din valuta. Ett öre fel och publiceringen förblir stängd, vilket är hela poängen med steget.
- swap the cart for a single Ceramic Travel Mug
| Artikel | Antal | Styckpris |
|---|---|---|
| Ceramic Travel Mug | 1 | $22.00 |
| Delsumma | $22.00 | |
| Summa | $22.00 | |
Utloggad, eller inloggad som kund utan taggar.
Scope targeting family tag (D9)
OmbyggtRiskabelt par D9. Den här filen och dess motpart ser nästan likadana ut och beter sig olika, så läs båda innan du avgör vilken du har.
Input.cart.line_items.each do |li|
next unless li.variant.product.tags.include?('sale')
li.change_line_price(li.original_line_price * (1.0 - (20 / 100.0)), message: "20% off sale")
end
Output.cart = Input.cartIdentifierat som Percentage off och ombyggt till ett erbjudande av typen volume. Det här är de verkliga konfigurationsnycklarna som erbjudandet sparas med, så du kan stämma av din egen ombyggnad fält för fält.
| Inställning | Värde | Konfigurationsnyckel |
|---|---|---|
| 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.
Byggd i vår publika demobutik, Meridian Goods, så att varje pris här går att räkna efter i stället för att tas på förtroende. Rabatten nedan är vad samma motor som kör i kassan räknar fram för exakt den här varukorgen.
| Artikel | Antal | Styckpris |
|---|---|---|
| Ceramic Travel Mug | 1 | $22.00 |
| Delsumma | $22.00 | |
| Rabatt | -$4.40 | |
| Summa | $17.60 | |
Utloggad, eller inloggad som kund utan taggar.
Före publicering frågar verktyget vad ditt gamla Script gjorde med en varukorg du redan har. Sätt den till 1 av Ceramic Travel Mug à $22.00, så är svaret givet.
Rabatten är $4.40. Summan som kunden betalade är $17.60.
Jämförelsen är exakt, ned till minsta enhet i din valuta. Ett öre fel och publiceringen förblir stängd, vilket är hela poängen med steget.
- swap the cart for a single Braided USB-C Cable
| Artikel | Antal | Styckpris |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Delsumma | $14.00 | |
| Summa | $14.00 | |
Utloggad, eller inloggad som kund utan taggar.
Två filer som ser likadana ut
Varje par nedan är samma Script-form skriven på två sätt. De skiljer sig åt på en eller två rader och gör olika saker, så läs båda innan du avgör vilken du har.
Farligt par D9
Scope targeting family tag (D9)
OmbyggtByggs om till ett volume-erbjudande.
Rabatt i sin egen varukorg: $4.40
next unless li.variant.product.tags.include?('sale')
li.change_line_price(li.original_line_price * (1.0 - (20 / 100.0)), message: "20% off sale")Flat percentage off all (D9)
OmbyggtByggs om till ett volume-erbjudande.
Rabatt i sin egen varukorg: $2.80
li.change_line_price(li.original_line_price * (1.0 - (20 / 100.0)), message: "20% off everything")Farligt par D13
Official dialect, product selector include (D13)
OmbyggtByggs om till ett volume-erbjudande.
Rabatt i sin egen varukorg: $4.40
product_selector_match_type: :include,
discount_message: "20% off sale items",
def type(line_item)
@selectors = @selectors.map { |selector| selector.downcase.strip }
(@selectors.include?(line_item.variant.product.product_type.downcase.strip)) == (@match_type == :include)
endOch 10 rader till som skiljer sig, på den egna sidan.
Official dialect, product selector exclude (D13)
OmbyggtByggs om till ett volume-erbjudande.
Rabatt i sin egen varukorg: $2.80
product_selector_match_type: :exclude,
discount_message: "20% off full-price items",Vanliga frågor
- 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äktade Script-former
One percentage off the whole catalogue, with no condition attached.
Already-marked-down products protected from a second discount.
Gift cards left out of a discount that would otherwise have hit them.
Klistra in ditt Script och se det byggas om
Ombyggnaden stäms av mot beloppet som ditt gamla Script tog ut innan du kan publicera den.