Shopify Theme Reviews

Shopify Cart Transform Function (2027): Build Bundles and Mix and Match

Fayyaz Ahmed

Shopify cart transform function for real bundles: one Function expands a kit into its products and merges a mix-and-match pair into a bundle at 15% off. Episode 5 of the Shopify Functions 2027 series (API 2027-01). Real store: the Night Skincare Kit ($84, $92 bought separately) arrives in checkout as its 3 products with the price split $34.70 + $32.87 + $16.43; Niacinamide ($32) + Retinol ($38) in the cart merge into one Serum Duo line at 15% off.

Chapters

  • 0:00 Intro: one kit in, three products out
  • 0:36 Why a cart transform
  • 1:08 One Function, two jobs
  • 1:45 The Function
  • 2:21 The bundle metafield
  • 2:48 The input query
  • 3:22 Expand
  • 3:55 The price split
  • 4:29 Merge
  • 5:06 Switch it on
  • 5:36 Two real errors
  • 6:11 Real store demo
  • 6:57 The real run
  • 7:28 Mix and match
  • 8:46 The limits
  • 9:30 Recap

Step by step (full walkthrough)

Intro: one kit in, three products out

I add one product to the cart: a skincare kit. And checkout receives the three real products inside it, at the kit price. A Shopify Function did that.

This is episode five of Shopify Functions 2027: bundles, with a Cart Transform Function. A kit that expands into its real products, with the price split correctly. 2 products that merge into one bundle line, at a discount.

Both jobs in one Function, because Shopify gives each app one cart transform. And two real errors I hit while switching it on.

Why a cart transform

In episodes two to four, our Functions changed prices. A discount never changes what is in the cart. A cart transform does.

It changes the lines themselves. Expand takes one line, like a kit, and turns it into the products inside it. Merge does the opposite: it takes separate lines and joins them into one bundle line.

And because the order holds the real products, inventory is counted on each component. The kit itself needs no stock.

One Function, two jobs

Here is the plan for our store. First row: the Night Skincare Kit, $84. Bought separately, its 3 products cost ninety-two.

Line expand turns the kit into those 3 products. Second row: mix and match. A customer adds the niacinamide serum and the retinol serum as two separate products.

Lines merge turns them into one Serum Duo line, 15% off. Why one Function for both? Shopify allows one cart transform function per app on each store.

If you need expand and merge, they live in the same Function.

The Function

shopify app generate extension, template cart transform, flavor vanilla JS. As in every episode, the template writes 2026, oh seven, so we pin 2027, oh one. Then function schema and typegen.

The handle is bundle builder. We will need that handle to switch the transform on. One target: cart transform run.

2027, oh one also has a prepare target that runs first and can compute input variables for run. We do not need it today. So: one target, one export, cart transform run.

The bundle metafield

Where does the kit say what is inside it? In a metafield on its variant: custom.bundle, type JSON. The merchant can see it and edit it in the admin.

Three components. Each has the variant ID, a quantity, and its normal retail price: thirty-eight, thirty-six and $18. We do not charge those retail prices.

We use them as weights, to split the kit price fairly. You will see why in a minute.

The input query

For each cart line we ask the ID, the quantity, and the price per unit. That price comes in the customer's currency. For product variants we ask the ID, and the bundle metafield, as a JSON value.

Normal products simply have no bundle metafield. And here is a field you only get in this API: cart transform. The transform object itself can carry metafields.

We keep the merge rules there. So the kit definition lives on the product, and the mix and match rules live on the cart transform.

Expand

For every line: skip it unless it is a variant with a bundle metafield. And we respect Shopify's limit: a quantity of 2,000 at most. Then the operation: line expand, with the cart line ID, and the expanded items.

Each one is a component variant, a quantity, and a fixed price per unit. That fixed price is how the customer pays $84 for the kit, not ninety-two. The prices come from a small helper, split price.

And that helper is where most bundle bugs live.

The price split

$84, split by the retail weights: thirty-eight, thirty-six and eighteen, out of ninety-two. We work in cents. The serum gets thirty-four seventy.

The face oil, thirty-two eighty-seven. If we rounded the roller on its own, rounding could leave the total a cent off. So the last component takes whatever is left: sixteen forty-three.

Thirty-four seventy, plus thirty-two eighty-seven, plus sixteen forty-three: exactly $84. Always let one item absorb the rounding.

Merge

Now mix and match. For each merge rule, we look for every component in the cart. If one is missing, no bundle.

The number of sets is the smallest quantity: two of one serum and one of the other make one duo. Then lines merge. The parent variant is a real, published product: Serum Duo.

The cart lines to join, and a price: percentage decrease, fifteen. A merged line costs the sum of its components, minus the percentage. Thirty-two plus thirty-eight is seventy, minus 15%: fifty-nine fifty.

Switch it on

Deploying is not enough. Like a discount, the transform must be created: cart transform create, with our function handle. The function ID argument is deprecated.

Block on failure, false: if our Function ever fails, Shopify ignores its output instead of blocking checkout. And here we attach the merge rules metafield in the same call. Our store answered with a new cart transform ID, and no userErrors.

But it took two tries to get here.

Two real errors

First error: access denied for the cart transforms field. The app had no cart transform scope. Add write cart transforms to the scopes in your app TOML, restart app dev, and the dev store grants it.

Second error: I ran the mutation a second time. Could not enable cart transform because it is already registered. Code: function already registered.

One app, one cart transform per store. To change it, update or delete the existing one. Both messages are copied from our store, word for word.

Real store demo

Now our development store. Here is the Night Skincare Kit, $84. A normal product, with no stock of its own.

The description says it: bought separately, $92. I add one kit to the cart. The cart shows one line, the kit, and under it the 3 products our Function expanded it into.

$84. In checkout: Night Skincare Kit, and a link that says hide three items. One retinol serum, one face oil, one face roller.

Total, $84. I stop here, before payment. When this order is placed, it holds the three real products, so each one leaves its own inventory.

The real run

Checkout does not print the component prices, so here is the real log of that run, from app dev. The input: one kit, eighty-four point zero. The output: line expand, with thirty-four seventy, thirty-two eighty-seven, and sixteen forty-three.

Exactly our split. And it used about 540,000 instructions, of the eleven million a Function may use. So the kit really is 3 products with three prices.

Now mix and match.

Mix and match

I start with an empty cart. The retinol night serum, $38. Into the cart.

One line, $38. Nothing to merge yet. Now the niacinamide serum, $32.

A separate product, added on its own. And the cart shows one line: Serum Duo, with both serums inside, fifty-nine fifty. That is $70, minus 15%.

Checkout agrees: Serum Duo, hide two items, fifty-nine fifty. In an earlier take, the store added the retinol serum twice. So the cart held two retinol and one niacinamide.

The Function merged one set into a Serum Duo, and the second retinol stayed a normal line, at $38. That is the smallest quantity rule at work: a set needs one of each. Here is that run's real log.

Retinol, quantity two. Niacinamide, quantity one. The output merges one of each into the Serum Duo variant, 15% off.

The rest of the retinol line is left alone. So the Function never takes more than a full set, and the customer still pays the normal price for the extra serum.

The limits

Before you sell this to a merchant, know the limits. Line update, the third operation, changes a line's title, price or image. Only development stores and Shopify Plus stores can use apps with line update.

Expand and merge have no such note. Quantities stop at two thousand. If a line has a selling plan, like a subscription, Shopify rejects all three operations.

If the Function fails, and block on failure is false, Shopify ignores the output. The cart still works, just without the bundle. And some surfaces do not run cart transforms at all: the Create Order API, Order Edit, and subscription renewals.

Recap

Let's recap. A cart transform changes what the lines are, not just their price. Line expand turns a kit into its products, and fixed prices make the split add up to the kit price.

Lines merge joins products into a bundle line, with a percentage off. One cart transform per app, so expand and merge share one Function. And to switch it on: the write cart transforms scope, then cart transform create.

The code is on GitHub, tag E P zero five, link in the description. Next episode: delivery customization, hiding and renaming shipping options at checkout. See you there.

The Shorts from this video

Shopify Bundles: One Kit, Three Products at Checkout (Function)

Shopify bundles with a Cart Transform Function: the Night Skincare Kit ($84) expands into its 3 real products at checkout, and the kit price is split across them so it always adds up.

Shopify Mix and Match Bundle: 2 Serums Become 1 Bundle at 15% Off

Shopify mix and match bundle with a Cart Transform Function: when both serums are in the cart, linesMerge turns them into one Serum Duo line at 15% off. The rule lives on the cart transform's metafield.

Shopify Cart Transform API: 2 Errors Before Your Bundle Works

Shopify cart transform API gotchas from a real store: 'Access denied for cartTransforms field' until the app has write_cart_transforms, and 'Could not enable cart transform because it is already registered' because each app gets ONE cart transform per store – so expand and merge live in one Function.

🔔 New episodes every week on the YouTube channel. Need a custom Shopify app or API integration? Wixob Technologies builds them: info@wixob.com.