Create a bundle app
The Shopify bundles sample app is an example of how to build a bundle app, and shows an implementation of a cart_transform
function to update the cart. It also shows how to do merge and expand transformations, and offers guidance on UI extensions to render bundles.
Anchor to What you'll learnWhat you'll learn
In this tutorial, you'll learn how to do the following tasks:
- Install the Shopify bundles sample app.
- Model a bundle by creating metafields on variants, creating a bundle parent product variant, and defining the bundle on child product variants.
- Enable the bundle through extensions.
Anchor to RequirementsRequirements
- You've created a Partner account.
- You've created a development store.
Anchor to Metafields versus line item propertiesMetafields versus line item properties
When you build custom bundles with cart transforms, you can store bundle data using metafields or line item properties. Choose metafields when you need secure, merchant-defined bundles with fixed compositions. Choose line item properties when you need flexible, buyer-customizable bundles, but ensure you implement proper validation and don't rely solely on the properties for critical business logic.
Metafields | Line item properties | |
---|---|---|
Best for | Fixed bundles where the bundle composition is predetermined by the merchant | Mix-and-match bundles where buyers can customize the bundle composition |
Pros |
|
|
Cons |
|
|
Anchor to Step 1: Install the Shopify bundles sample appStep 1: Install the Shopify bundles sample app
The sample app is currently published under the Shopify organization, and available for installation on stores.
-
Navigate to the Shopify bundles sample app's installation URL, and enter your development store's storefront URL into the text box.
You're redirected to the Shopify admin for the store and prompted to install the app.
-
Click Install app.
Anchor to Step 2: Model a bundleStep 2: Model a bundle
You model a bundle by creating metafields on variants, creating a bundle parent product variant, and defining the bundle on child product variants.
Anchor to Create metafields on product variantsCreate metafields on product variants
-
In the Shopify admin, go to Settings.
-
Click Custom data in the side navigation.
NoteThe button might say Metafields depending on your store's settings.
-
Under Metafields, click Variants.
-
Click Add definition to create the following metafield definitions and click Save:
component_reference
metafield definition:Field Value Name component_reference
Namespace and key custom.component_reference
(Don't change the default)Description Components included in Bundle
Select type Product Variant
-List of product variants
component_quantities
metafield definition:Field Value Name component_quantities
Namespace and key custom.component_quantities
(Don't change the default)Description Quantity of components included in Bundle
Select type Integer
-List of values
Validation Minimum value
-1
component_parents
metafield definition:Field Value Name component_parents
Namespace and key custom.component_parents
(Don't change the default)Description Child component parent definition
Select type JSON
Rules Refer to the JSON rules example. JSON rules
{"$schema": "https://json-schema.org/draft/2020-12/schema","title": "Bundle component parents","description": "A definition of the bundle a child belongs to","type": "array","items":{"type": "object","properties": {"id": {"description": "ID of bundle parent product variant: gid://shopify/ProductVariant/<id number>","type": "string"},"component_reference": {"type": "object","properties": {"value": {"description": "Array of product variant IDs: [gid://shopify/ProductVariant/<id number>]","type": "array","items": {"type": "string"},"minItems": 1,"uniqueItems": true}}},"component_quantities": {"type": "object","properties": {"value": {"description": "Array of quantities of product variants defined in component_reference: [1]","type": "array","items": {"type": "integer"},"minItems": 1}}}}},"required": ["id","component_reference","component_quantities"]}
Anchor to Create a bundle parent product variantCreate a bundle parent product variant
-
Create 2 separate products that will be a part of the bundle. The products should have the following requirements:
- At least one available in inventory
- Price is more than 0
- At least one option, since you'll need access to the product variant
-
Create a product that represents the bundle with the following requirements:
- At least one available in inventory
- Price is more than 0
- At least one option, since you'll need access to the product variant.
-
After saving the product, click Edit next to the variant of the option. This will be a bundle parent product variant.
-
Scroll to the Metafields section of the variant edit page.
-
Open up the product variants that will be included in the bundle in separate browser tabs.
NoteYou'll need access to the product variant IDs of the bundled products while defining them. The product variant IDs can be found in the URL of the product variant:
https://admin.shopify.com/store/<your store>/products/<product ID>/variants/<product variant ID>
. -
On the parent product variant page, click the
component_reference
field under Metafields. -
Select the product variants that will be bundled, and then click Save.
-
Select the
component_quantities
field under Metafields.NoteAdd a quantity for each
component_reference
in the bundle. The order matters, as the quantity entered will correspond with thecomponent_reference
entered. -
Click Save on the parent product variant page.
CautionDon't exit this page, as you'll need the ID from the URL.
Anchor to Define the bundle on child product variantsDefine the bundle on child product variants
Complete the following steps for each children product variant defined in component_reference
in the previous step:
-
On the child product variant page, click the
component_parents
field under Metafields. -
Using the JSON schema defined, build the bundle definition.
NoteThe
component_reference
andcomponent_quantities
values must match the values defined on the parent in the previous step.
Example
Anchor to Step 3: Enable the bundle through extensionsStep 3: Enable the bundle through extensions
The bundle-cart-transform
extension uses the cart_transform
function in Shopify to update the cart. The sample app uses the extension to do two transformations:
- Merge the bundle
- Expand the bundle
Anchor to Merge the bundleMerge the bundle
If the extension detects all of the components of a given bundle in the cart at checkout, then the extension returns linesMerge
operations to Shopify that combine the respective components and present the given parent_product_variant
.
In the reference app, the linesMerge
functionality reads over all of the product_variants
, and looks for the metafield
with the following properties: namespace: "custom", key: "component_parents"
.
This query is defined in extensions/bundle-cart-transform/src/run.graphql
:
Query
component_parents
is a metafield
on product_variant
with the content type JSON
. Its value
is an array of objects defining the bundle rules. For the linesMerge
function to work in the sample app, each child belonging to a bundle must have component_parents
defined. A bundle rule has the following shape:
Bundle rule
For example, you might have a bundle that contains two shirts and one pair of pants. The bundle parent product_variant
ID is 6
, the shirt product_variant
ID included in the bundle is 3
and the pants product_variant
ID included in the bundle is 4
.
The following example shows the value of the component_parents
metafield that must belong to both the shirt product_variant
and pants product_variant
for the linesMerge
functionality in the extension to work.
Example
The order of the component_reference
and the component_quantities
match up. In the example, the first component_quantities
value (2
) matches with the first component_reference
value (gid://shopify/ProductVariant/3
) because two shirts are required for the bundle to be complete.
Notice that the extension uses Shopify's product_variant
GIDs. The ID of the product_variant
must be prepended with gid://shopify/ProductVariant/
to denote that it's a product_variant
ID.
Anchor to Rendering bundles in storefrontRendering bundles in storefront
Bundles apps are responsible for managing product variants and options. Bundles apps need to create user interface product management flows to do the following:
-
Create a new product. At least one bundle configuration must be created.
-
Update an existing product.
These pages must be implemented using Shopify App Bridge.
Anchor to Creating an app blockCreating an app block
If your bundles app needs to customize the storefront, then you need to create an app block that encapsulates that logic.
Learn how to create an app block using theme app extensions.
Anchor to Next stepsNext steps
- Learn more about how Shopify Functions work and the benefits of using Shopify Functions.
- Consult the API references for Shopify Functions.
- Learn how to use variables in your input query.