Bundles
Apps/Recipes/Bundles

Sell product bundles with a Shopify Function

A variant the store sells, expanded at checkout into the variants it's made of, with an optional discount.

Shopify, customScopes write_productswrite_cart_transformsRecipe id bundler

What you'll build

Bundles pagevariant and componentsbundle.createpublishes the bundleVariant metafield$app:bundles.componentsCart transformlineExpand at checkout

What it shows: A Shopify Function in an app: written in extensions/, built with the Shopify CLI and released by Inference Bureau with the rest of the app, fed by metafields the app's actions write, and turned on for each store on install.

Before you start

  • The CLI: npm i -g @inferencebureau/apps, then ib-apps login once.
  • A Shopify Partner organization, and a development store to install on.

Create the app from the recipe

One command creates the app on Inference Bureau and downloads its files into ./my-bundler. Shopify recipes need --platform shopify.

ib-apps create my-bundler --platform shopify --recipe bundler

Tour what the recipe loaded

The recipe lies over the Shopify starter: files of the same path are replaced, and the starter's storefront block is dropped. Here's where each piece lives.

WhereWhat's there
api/models/bundle/The bundle model: the variant it's sold as, its components and a discount; one bundle per variant.
api/lib/bundles.tsSaving publishes the bundle to its variant's $app:bundles.components metafield; deleting takes it down.
extensions/bundler/The cart transform Function reads each line's variant metafield in its input query and answers lineExpand for each bundle.
shopifyShop/actions/install.ts, reinstall.tsRun cartTransformCreate with the Function's handle, so it runs on the store's carts.
web/app/bundles.tsx, web/app/api/The admin page finds the bundle product and what's in it with a search of the store's products.
tests/bundler.test.tsThe actions, and the Function's own code on sample carts.
// extensions/bundler/src/cart_transform_run.js: a bundle line becomes its components at checkout.
export function cartTransformRun(input) {
  const operations = [];
  for (const line of input.cart.lines) {
    const bundle = line.merchandise.bundle?.jsonValue; // $app:bundles.components on the variant
    const items = (bundle?.components ?? []).map((c) => ({ merchandiseId: c.variantId, quantity: c.quantity }));
    if (!items.length) continue;
    operations.push({ lineExpand: { cartLineId: line.id, expandedCartItems: items, price: { percentageDecrease: { value: bundle.discountPercent } } } });
  }
  return { operations };
}

Check what it set on the app

Nothing to do here: this is what the recipe configured, so you know where to look in Settings later.

PlatformShopify, custom
Scopeswrite_products, write_cart_transforms
Admin pagesInference Bureau

Connect Shopify and install

  1. Create the Shopify app in your Partner organization
  2. The first release builds the Function (about a minute) and lists it under Extensions
  3. Install on a development store: the install runs cartTransformCreate

Run it

ib-apps dev syncs both ways while you work and runs the development server. Saving anything under extensions/ releases it to the development Shopify app by itself.

cd my-bundler
ib-apps dev

Logs show a succeeded config release once the extension is live.

Try it end to end

0 of 4 checked

Make it yours

Change the Function

extensions/bundler/src/cart_transform_run.js and its input query beside it.

More extensions

See Shopify extensions for the other kinds an app can release.

Ship it

ib-apps test runs its tests, then ib-apps deploy sends development to production. See Deploys.