Skip to content
Scripts examples

Migrating a buy one get one free script

Shopify shipped this as a starter template, so it is everywhere. The line that matters divides the quantity by the paid units PLUS the free one, which is what makes a nine-unit cart earn two free instead of three.

Rebuilt
What the Script did

It split the free units off each complete set and repriced them to zero.

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.

The Script

Rebuilt

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

The original Ruby

BUY = 3
GET = 1

Input.cart.line_items.each do |line_item|
  sets = line_item.quantity / (BUY + GET)
  free_count = sets * GET
  next if free_count == 0
  discounted = line_item.split(take: free_count)
  discounted.change_line_price(Money.zero, message: "Buy 3 get 1 free")
  Input.cart.line_items << discounted
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 3 -> free itemtiers[0]
Buy quantity3bxgy.buyQty
Get quantity1bxgy.getQty
Get selectionsame_itembxgy.getSelection
Reward on the get units100% offbxgy.getDiscountPct
Free units are additionaltruebxgy.freeUnitsAreAdditional
Target scopealltarget.scope
MethodautomaticdiscountMethod
Allocationstandardstacking.allocation
Combines with other discountsnostacking
You will be asked to confirm
  • Confirm which products this applies to. We could not read product targeting from the script.
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
Braided USB-C Cable4$14.00
Subtotal$56.00
Discount-$14.00
Total$42.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 4 of Braided USB-C Cable at $14.00 and the answer is fixed.

The discount is $14.00. The total the shopper paid is $42.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
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 D3

Bogo same item free (D3)

Rebuilt

Rebuilt as a bxgy offer.

Discount on its own cart: $14.00

Lines that differ
  sets = line_item.quantity / (BUY + GET)
Read Buy one get one free

Bogo inclusive basis (D3)

Rebuilt

Rebuilt as a bxgy offer.

Discount on its own cart: $14.00

Lines that differ
  sets = line_item.quantity / BUY
Read Buy X get Y, inclusive

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?
4 x Braided USB-C Cable at $14.00. The engine takes $14.00 off, and checkout charges $42.00.
What will I have to check before publishing?
Confirm which products this applies to. We could not read product targeting from the script.

Related Script shapes

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.

Percentage off the cheapest item

The least expensive thing in the cart discounted, once.

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.