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.
What you'll build
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, thenib-apps loginonce. - 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 bundlerTour 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.
| Where | What'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.ts | Saving 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.ts | Run 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.ts | The 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.
write_products, write_cart_transformsConnect Shopify and install
- Create the Shopify app in your Partner organization
- The first release builds the Function (about a minute) and lists it under Extensions
- 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 devLogs show a succeeded config release once the extension is live.
Try it end to end
Make it yours
extensions/bundler/src/cart_transform_run.js and its input query beside it.
See Shopify extensions for the other kinds an app can release.
ib-apps test runs its tests, then ib-apps deploy sends development to production. See Deploys.