Skip to content
Scripts examples

Migrating a buy X get Y script on an inclusive basis

Divide by the paid units alone rather than by paid plus free, and every third unit becomes free instead of every fourth. The two files differ by three characters and by half your giveaway.

Rebuilt
What the Script did

It gave one free unit for every group of paid units, counting the free one inside the group.

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
  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 additionalfalsebxgy.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 Cable3$14.00
Subtotal$42.00
Discount-$14.00
Total$28.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 Braided USB-C Cable at $14.00 and the answer is fixed.

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

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

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 Braided USB-C Cable at $14.00. The engine takes $14.00 off, and checkout charges $28.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 one get one free

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

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.