Skip to content
Scripts examples

Migrating a BOGO that ran across the whole cart

How most agency-written BOGOs worked: pool every eligible unit, sort it, then peel off the discounted ones. Sort ascending and the cheapest units are free. Sort descending and they are not. The class names differ by one word.

Rebuilt
What the Script did

It pooled every eligible unit in the cart, sorted them by price, and discounted a share of them.

Merchants wrote this 2 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.

Bogo cross line interleaved (D17)

Rebuilt

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

The original Ruby

class InterleavedPartitioner
  def initialize(paid_item_count, discounted_item_count)
    @paid_item_count = paid_item_count
    @discounted_item_count = discounted_item_count
  end

  def partition(cart, applicable_line_items)
    sorted_items = applicable_line_items.sort_by { |line_item| line_item.variant.price }.reverse
    discounted_items = []
    paid_items_seen = 0
    discounted_items_seen = 0

    sorted_items.each do |line_item|
      line_item.quantity.times do
        if paid_items_seen < @paid_item_count
          paid_items_seen += 1
        else
          discounted_items_seen += 1
          discounted_items.push(line_item.split(take: 1))
          if discounted_items_seen == @discounted_item_count
            paid_items_seen = 0
            discounted_items_seen = 0
          end
        end
      end
    end

    discounted_items
  end
end

CAMPAIGNS = [
  BogoCampaign.new(
    ProductTagSelector.new("bogo"),
    PercentageDiscount.new(100, "Buy 2 get 1 free"),
    InterleavedPartitioner.new(2, 1)
  ),
]

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

Output.cart = Input.cart
What it becomes

Recognised as Buy X, get Y and rebuilt as a bxgy 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 typebxgytype
Countingper_groupcounting
Recurrencerepeatrecurrence
Tier 1min qty 2 -> free itemtiers[0]
Buy quantity2bxgy.buyQty
Get quantity1bxgy.getQty
Get selectionsame_itembxgy.getSelection
Reward on the get units100% offbxgy.getDiscountPct
Free units are additionaltruebxgy.freeUnitsAreAdditional
Target scopetagstarget.scope
Target includesbogotarget.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.
You will be told
  • Your Script used Shopify's own item ordering to choose what to discount. We use the campaign's allocation order, which can pick a different item in a mixed cart.
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.

  • No Meridian Goods product carries the tag bogo. Set it on Aria Wireless Earbuds in Shopify before you build the cart, and on nothing else.
The cart that triggers this offer, and what it costs
ItemQtyUnit price
Aria Wireless Earbuds3$79.00
Subtotal$237.00
Discount-$79.00
Total$158.00

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 3 of Aria Wireless Earbuds at $79.00 and the answer is fixed.

The discount is $79.00. The total the shopper paid is $158.00.

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
  • drop to 1 of the same product, under what the rule needs
The cart that must not trigger this offer
ItemQtyUnit price
Aria Wireless Earbuds1$79.00
Subtotal$79.00
Total$79.00

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

Bogo cross line low to high (D17)

Rebuilt

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

The original Ruby

class LowToHighPartitioner
  def initialize(paid_item_count, discounted_item_count)
    @paid_item_count = paid_item_count
    @discounted_item_count = discounted_item_count
  end

  def partition(cart, applicable_line_items)
    sorted_items = applicable_line_items.sort_by { |line_item| line_item.variant.price }
    total_applicable_items = sorted_items.map(&:quantity).reduce(0, :+)
    discounted_items_to_find = (total_applicable_items / (@paid_item_count + @discounted_item_count)) * @discounted_item_count
    discounted_items = []

    sorted_items.each do |line_item|
      break if discounted_items_to_find <= 0
      take = [line_item.quantity, discounted_items_to_find].min
      discounted_items.push(line_item.split(take: take))
      discounted_items_to_find -= take
    end

    discounted_items
  end
end

CAMPAIGNS = [
  BogoCampaign.new(
    ProductTagSelector.new("bogo"),
    PercentageDiscount.new(100, "Buy 2 get 1 free"),
    LowToHighPartitioner.new(2, 1)
  ),
]

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

Output.cart = Input.cart
What it becomes

Recognised as Buy X, get Y and rebuilt as a bxgy 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 typebxgytype
Countingper_groupcounting
Recurrencerepeatrecurrence
Tier 1min qty 2 -> free itemtiers[0]
Buy quantity2bxgy.buyQty
Get quantity1bxgy.getQty
Get selectioncheapest_eligiblebxgy.getSelection
Reward on the get units100% offbxgy.getDiscountPct
Free units are additionaltruebxgy.freeUnitsAreAdditional
Target scopetagstarget.scope
Target includesbogotarget.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.

  • No Meridian Goods product carries the tag bogo. Set it on Aria Wireless Earbuds and Impact Phone Case in Shopify before you build the cart, and on nothing else.
The cart that triggers this offer, and what it costs
ItemQtyUnit price
Impact Phone Case2$24.00
Aria Wireless Earbuds1$79.00
Subtotal$127.00
Discount-$24.00
Total$103.00

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 3 of Impact Phone Case at $24.00 and the answer is fixed.

The discount is $24.00. The total the shopper paid is $48.00.

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
  • drop to 1 of the same product, under what the rule needs
The cart that must not trigger this offer
ItemQtyUnit price
Impact Phone Case1$24.00
Subtotal$24.00
Total$24.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 D17

Bogo cross line interleaved (D17)

Rebuilt

Rebuilt as a bxgy offer.

Discount on its own cart: $79.00

Lines that differ
class InterleavedPartitioner
    sorted_items = applicable_line_items.sort_by { |line_item| line_item.variant.price }.reverse
    paid_items_seen = 0
    discounted_items_seen = 0
      line_item.quantity.times do
        if paid_items_seen < @paid_item_count

And 11 more differing lines, on its own page.

Read BOGO across the cart

Bogo cross line low to high (D17)

Rebuilt

Rebuilt as a bxgy offer.

Discount on its own cart: $24.00

Lines that differ
class LowToHighPartitioner
    sorted_items = applicable_line_items.sort_by { |line_item| line_item.variant.price }
    total_applicable_items = sorted_items.map(&:quantity).reduce(0, :+)
    discounted_items_to_find = (total_applicable_items / (@paid_item_count + @discounted_item_count)) * @discounted_item_count
      break if discounted_items_to_find <= 0
      take = [line_item.quantity, discounted_items_to_find].min

And 3 more differing lines, on its own page.

Read BOGO across the cart

Common questions

Will Stackable rebuild this script?
Yes. Every spelling on this page is rebuilt today as a bxgy offer, and the cart above is the one that proves it.
What cart proves it works?
3 x Aria Wireless Earbuds at $79.00. The engine takes $79.00 off, and checkout charges $158.00.
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. Your Script used Shopify's own item ordering to choose what to discount. We use the campaign's allocation order, which can pick a different item in a mixed cart.

Related Script shapes

Buy one get one free

A set of paid units earns an extra unit at no charge.

Buy X get Y, inclusive

The same buy-and-get offer, counted so the reward sits inside each set rather than beside it.

Limit the discounted units

The discount applied to the first few units only, with the rest at full price.

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.