Skip to main content
Watch the cart total and keep a free gift in sync with it: add the gift once the shopper crosses a spend threshold, remove it if they drop back below.
For a merchant-configured version with tiers, per-market rules, and a progress bar, use the Rewards block, with no code. This is for when your rule is something the block can’t express.

The snippet

Paste into Cart settings → Custom script → Initialization, and set the two constants:

How it works

reconcileGift describes the state the cart should be in, then makes at most one change to get there:
  • Qualifies and the gift isn’t there → addItem adds it.
  • Doesn’t qualify and the gift is there → removeItem removes it, by the line’s key.
  • Otherwise → do nothing.
It runs on both cart_loaded (so a cart that already qualifies on page load gets the gift) and cart_updated (so it reacts to every change afterward). Because cart_loaded replays to late subscribers, this works no matter when your script runs.
The !gift / gift guards are what stop this looping. Adding the gift fires cart_updated, which runs reconcileGift again, and the second pass finds the gift already present, so it does nothing. Strip the guards and you get an infinite loop. See the two rules.
Note that adding the gift raises the cart total, so a threshold near a product’s price can oscillate: adding a $0 gift is safe, but a gift with a price could push the cart above the threshold on its own. Keep the gift free, or compare against a total that excludes it.

Adapting it

Gate on item count instead of spend:
Gate on a specific product being in the cart:
Only in certain countries. Read context, which is available before the cart loads:
Exclude the gift from the threshold so it can’t hold itself up:
Mark the gift so it reads as a gift. A line transform can change a line’s title, variant title, hidden state, and internal properties — so relabel it:
A line transform cannot hide a line’s quantity controls — its only setters are setTitle, setVariantTitle, setHidden, and setInternalProperties. If a shopper changes the gift’s quantity, correct it from a cart_updated handler instead.

Where to go next