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.
RebuiltIt skipped every line whose product did not carry the listed tag, vendor or type.
Merchants wrote this 3 different ways, and they do not all mean the same thing. Each spelling is below with its own cart and its own numbers.
Paste it into Scripts rescue and watch what it rebuilds
- Copy the Ruby with the button beside any Script on this page.
- In your Shopify admin, open Stackable, then Tools, then Scripts rescue.
- Paste it there, read what the tool says it will build, and compare that with the settings on this page.
Scripts rescue is on the Pro plan. Nothing is published until you tell it what your old Script charged and the two figures match to the cent.
Official dialect, product selector include (D13)
RebuiltDanger pair D13. This file and its partner look almost identical and behave differently, so read both before you decide which one you have.
# ================================ 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.cartRecognised as Percentage off and rebuilt as a volume offer. These are the real configuration keys the offer is saved with, so you can check your own rebuild against them field by field.
| Setting | Value | Config key |
|---|---|---|
| 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.
Built on our public demo store, Meridian Goods, so every price here can be reproduced rather than taken on trust. The discount below is what the same engine that runs at checkout computes for this exact cart.
| Item | Qty | Unit price |
|---|---|---|
| Ceramic Travel Mug | 1 | $22.00 |
| Subtotal | $22.00 | |
| Discount | -$4.40 | |
| Total | $17.60 | |
Signed out, or signed in as a customer with no tags.
Before publishing, the tool asks what your old Script did to a cart you already have. Set it to 1 of Ceramic Travel Mug at $22.00 and the answer is fixed.
The discount is $4.40. The total the shopper paid is $17.60.
The comparison is exact, down to the smallest unit of your currency. One cent out and publishing stays shut, which is the whole point of the step.
- swap the cart for a single Braided USB-C Cable
| Item | Qty | Unit price |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Subtotal | $14.00 | |
| Total | $14.00 | |
Signed out, or signed in as a customer with no tags.
Official dialect, product selector exclude (D13)
RebuiltDanger pair D13. This file and its partner look almost identical and behave differently, so read both before you decide which one you have.
# ================================ 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.cartRecognised as Percentage off and rebuilt as a volume offer. These are the real configuration keys the offer is saved with, so you can check your own rebuild against them field by field.
| Setting | Value | Config key |
|---|---|---|
| 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.
Built on our public demo store, Meridian Goods, so every price here can be reproduced rather than taken on trust. The discount below is what the same engine that runs at checkout computes for this exact cart.
- 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.
| Item | Qty | Unit price |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Subtotal | $14.00 | |
| Discount | -$2.80 | |
| Total | $11.20 | |
Signed out, or signed in as a customer with no tags.
Before publishing, the tool asks what your old Script did to a cart you already have. Set it to 1 of Braided USB-C Cable at $14.00 and the answer is fixed.
The discount is $2.80. The total the shopper paid is $11.20.
The comparison is exact, down to the smallest unit of your currency. One cent out and publishing stays shut, which is the whole point of the step.
- swap the cart for a single Ceramic Travel Mug
| Item | Qty | Unit price |
|---|---|---|
| Ceramic Travel Mug | 1 | $22.00 |
| Subtotal | $22.00 | |
| Total | $22.00 | |
Signed out, or signed in as a customer with no tags.
Scope targeting family tag (D9)
RebuiltDanger pair D9. This file and its partner look almost identical and behave differently, so read both before you decide which one you have.
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.cartRecognised as Percentage off and rebuilt as a volume offer. These are the real configuration keys the offer is saved with, so you can check your own rebuild against them field by field.
| Setting | Value | Config key |
|---|---|---|
| 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.
Built on our public demo store, Meridian Goods, so every price here can be reproduced rather than taken on trust. The discount below is what the same engine that runs at checkout computes for this exact cart.
| Item | Qty | Unit price |
|---|---|---|
| Ceramic Travel Mug | 1 | $22.00 |
| Subtotal | $22.00 | |
| Discount | -$4.40 | |
| Total | $17.60 | |
Signed out, or signed in as a customer with no tags.
Before publishing, the tool asks what your old Script did to a cart you already have. Set it to 1 of Ceramic Travel Mug at $22.00 and the answer is fixed.
The discount is $4.40. The total the shopper paid is $17.60.
The comparison is exact, down to the smallest unit of your currency. One cent out and publishing stays shut, which is the whole point of the step.
- swap the cart for a single Braided USB-C Cable
| Item | Qty | Unit price |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| Subtotal | $14.00 | |
| Total | $14.00 | |
Signed out, or signed in as a customer with no tags.
Two files that look the same
Each pair below is one Script shape written two ways. They differ by a line or two and they do different things, so read both before deciding which one you have.
Danger pair D9
Scope targeting family tag (D9)
RebuiltRebuilt as a volume offer.
Discount on its own cart: $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)
RebuiltRebuilt as a volume offer.
Discount on its own cart: $2.80
li.change_line_price(li.original_line_price * (1.0 - (20 / 100.0)), message: "20% off everything")Danger pair D13
Official dialect, product selector include (D13)
RebuiltRebuilt as a volume offer.
Discount on its own cart: $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)
endAnd 10 more differing lines, on its own page.
Official dialect, product selector exclude (D13)
RebuiltRebuilt as a volume offer.
Discount on its own cart: $2.80
product_selector_match_type: :exclude,
discount_message: "20% off full-price items",Common questions
- 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.
Related Script shapes
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.
Paste your Script and see it rebuilt
The rebuild is checked against the money your old Script charged before you can publish it.