Skip to content
Scripts examples

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.

Rebuilt
What the Script did

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

Run it against your own store

Paste it into Scripts rescue and watch what it rebuilds

  1. Copy the Ruby with the button beside any Script on this page.
  2. In your Shopify admin, open Stackable, then Tools, then Scripts rescue.
  3. 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)

Rebuilt

Danger pair D13. This file and its partner look almost identical and behave differently, so read both before you decide which one you have.

The original Ruby

# ================================ 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.cart
What it becomes

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

Every setting, spelled out
SettingValueConfig key
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
You will be asked to confirm
  • 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 cart that proves it

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.

The cart that triggers this offer, and what it costs
ItemQtyUnit price
Ceramic Travel Mug1$22.00
Subtotal$22.00
Discount-$4.40
Total$17.60

Signed out, or signed in as a customer with no tags.

The figure the tool checks you against

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.

The cart that must produce nothing
  • swap the cart for a single Braided USB-C Cable
The cart that must not trigger this offer
ItemQtyUnit price
Braided USB-C Cable1$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)

Rebuilt

Danger pair D13. This file and its partner look almost identical and behave differently, so read both before you decide which one you have.

The original Ruby

# ================================ 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.cart
What it becomes

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

Every setting, spelled out
SettingValueConfig key
Offer typevolumetype
Countingper_productcounting
Recurrenceoncerecurrence
Tier 1min qty 1 -> 20% offtiers[0]
Target scopealltarget.scope
Target excludessaletarget.excludeIds
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
You will be asked to confirm
  • 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 cart that proves it

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.
The cart that triggers this offer, and what it costs
ItemQtyUnit price
Braided USB-C Cable1$14.00
Subtotal$14.00
Discount-$2.80
Total$11.20

Signed out, or signed in as a customer with no tags.

The figure the tool checks you against

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.

The cart that must produce nothing
  • swap the cart for a single Ceramic Travel Mug
The cart that must not trigger this offer
ItemQtyUnit price
Ceramic Travel Mug1$22.00
Subtotal$22.00
Total$22.00

Signed out, or signed in as a customer with no tags.

Scope targeting family tag (D9)

Rebuilt

Danger pair D9. This file and its partner look almost identical and behave differently, so read both before you decide which one you have.

The original Ruby

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.cart
What it becomes

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

Every setting, spelled out
SettingValueConfig key
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
You will be asked to confirm
  • 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 cart that proves it

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.

The cart that triggers this offer, and what it costs
ItemQtyUnit price
Ceramic Travel Mug1$22.00
Subtotal$22.00
Discount-$4.40
Total$17.60

Signed out, or signed in as a customer with no tags.

The figure the tool checks you against

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.

The cart that must produce nothing
  • swap the cart for a single Braided USB-C Cable
The cart that must not trigger this offer
ItemQtyUnit price
Braided USB-C Cable1$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)

Rebuilt

Rebuilt as a volume offer.

Discount on its own cart: $4.40

Lines that differ
  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")
Read Scoped to a tag or vendor

Flat percentage off all (D9)

Rebuilt

Rebuilt as a volume offer.

Discount on its own cart: $2.80

Lines that differ
  li.change_line_price(li.original_line_price * (1.0 - (20 / 100.0)), message: "20% off everything")
Read Sitewide percentage off

Danger pair D13

Official dialect, product selector include (D13)

Rebuilt

Rebuilt as a volume offer.

Discount on its own cart: $4.40

Lines that differ
    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)
  end

And 10 more differing lines, on its own page.

Read Scoped to a tag or vendor

Official dialect, product selector exclude (D13)

Rebuilt

Rebuilt as a volume offer.

Discount on its own cart: $2.80

Lines that differ
    product_selector_match_type: :exclude,
    discount_message: "20% off full-price items",
Read Scoped to a tag or vendor

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

Sitewide percentage off

One percentage off the whole catalogue, with no condition attached.

Exclude sale items

Already-marked-down products protected from a second discount.

Exclude gift cards

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.

We use essential cookies to run this site, and, only with your permission, analytics cookies to understand traffic. Read our Cookie Policy.