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.
RebuiltIt 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.
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.
Bogo cross line interleaved (D17)
RebuiltDanger pair D17. This file and its partner look almost identical and behave differently, so read both before you decide which one you have.
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.cartRecognised 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.
| Setting | Value | Config key |
|---|---|---|
| Offer type | bxgy | type |
| Counting | per_group | counting |
| Recurrence | repeat | recurrence |
| Tier 1 | min qty 2 -> free item | tiers[0] |
| Buy quantity | 2 | bxgy.buyQty |
| Get quantity | 1 | bxgy.getQty |
| Get selection | same_item | bxgy.getSelection |
| Reward on the get units | 100% off | bxgy.getDiscountPct |
| Free units are additional | true | bxgy.freeUnitsAreAdditional |
| Target scope | tags | target.scope |
| Target includes | bogo | 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.
- 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.
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.
| Item | Qty | Unit price |
|---|---|---|
| Aria Wireless Earbuds | 3 | $79.00 |
| Subtotal | $237.00 | |
| Discount | -$79.00 | |
| Total | $158.00 | |
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 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.
- drop to 1 of the same product, under what the rule needs
| Item | Qty | Unit price |
|---|---|---|
| Aria Wireless Earbuds | 1 | $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)
RebuiltDanger pair D17. This file and its partner look almost identical and behave differently, so read both before you decide which one you have.
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.cartRecognised 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.
| Setting | Value | Config key |
|---|---|---|
| Offer type | bxgy | type |
| Counting | per_group | counting |
| Recurrence | repeat | recurrence |
| Tier 1 | min qty 2 -> free item | tiers[0] |
| Buy quantity | 2 | bxgy.buyQty |
| Get quantity | 1 | bxgy.getQty |
| Get selection | cheapest_eligible | bxgy.getSelection |
| Reward on the get units | 100% off | bxgy.getDiscountPct |
| Free units are additional | true | bxgy.freeUnitsAreAdditional |
| Target scope | tags | target.scope |
| Target includes | bogo | 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.
- 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.
| Item | Qty | Unit price |
|---|---|---|
| Impact Phone Case | 2 | $24.00 |
| Aria Wireless Earbuds | 1 | $79.00 |
| Subtotal | $127.00 | |
| Discount | -$24.00 | |
| Total | $103.00 | |
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 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.
- drop to 1 of the same product, under what the rule needs
| Item | Qty | Unit price |
|---|---|---|
| Impact Phone Case | 1 | $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)
RebuiltRebuilt as a bxgy offer.
Discount on its own cart: $79.00
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_countAnd 11 more differing lines, on its own page.
Bogo cross line low to high (D17)
RebuiltRebuilt as a bxgy offer.
Discount on its own cart: $24.00
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].minAnd 3 more differing lines, on its own page.
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
A set of paid units earns an extra unit at no charge.
The same buy-and-get offer, counted so the reward sits inside each set rather than beside it.
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.