Bundled Products Recipe
This recipe provides the general steps to implement bundled products in your Medusa application.
Overview#
Bundled products allow you to group multiple products into a single bundle that customers can purchase together. By using bundled products, you can offer items at a discounted price or fulfill items within the same bundle separately, among other features.
Medusa provides an inventory kit feature that allows you to create bundled products. However, it doesn't support all bundled-product features. For example, you can't set a different price for the bundle, or fulfill items within the same bundle separately.
To support more bundled-product features, you can customize the Medusa application by creating a Bundled Product Module and building flows around it.
Create Bundled Product Module#
Your custom features and functionalities are implemented inside modules. The module is integrated into the Medusa application without any implications on existing functionalities.
The module will hold your custom data models and the service implementing bundled-product-related features.
Create Custom Data Models#
A data model represents a table in the database. You can define in your module data models to store data related to your custom features, such as a bundled product.
For example, you can define:
- A
Bundle
model for the bundle itself. - A
BundleItem
model for the items in the bundle.
Then, you can link your custom data model to data models from other modules. For example, you can link the BundleItem
model to the Product Module's Product
data model.
Implement Data Management Features#
Your module’s main service holds data-management and other related features. Then, in other resources, such as an API route, you can resolve the service from the Medusa container and use its functionalities.
Medusa facilitates implementing data-management features using the service factory. Your module's main service can extend this service factory, and it generates data-management methods for your data models.
Implement Workflows#
You implement the features in your use case using workflows. A workflow is a series of queries and actions, called steps, that complete a task.
By using workflows, you benefit from features like rollback mechanism, error handling, and retrying failed steps.
You can implement workflows that create a bundled product, add bundled product to the cart, and more. In the workflow's steps, you can resolve the Bundled Product Module's service and use its data-management methods to manage bundled products.
Then, you can utilize these workflows in other resources, such as an API route.
Add Custom API Routes#
API routes expose your features to external applications, such as the admin dashboard or the storefront.
You can create custom API routes that expose the features you've built as workflows. For example, you can create an API route that allows merchants to list and create bundled products.
Manage Linked Records#
If you've defined links between data models of two modules, you can manage them through two tools: Link and Query.
Use Link to create a link between two records, and use Query to fetch data across linked data models.
For example, you can define a link between a Bundle
and a Product
from the Product Module. Later, you can retrieve the product associated with the bundle using Query.
Customize Admin Dashboard#
You can extend the Medusa Admin to provide merchants with an interface to manage bundled products. You can inject widgets into existing pages or create new pages.
In your customizations, you send requests to the API routes you created to manage bundled products.
Customize or Build Storefront#
Customers use your storefront to browse your bundled products and purchase them. You can also provide other helpful features, such as displaying the bundle's details and allowing customers to select options for the items in the bundle.
Medusa provides a Next.js Starter Storefront with standard commerce features including listing products, placing orders, and managing accounts. You can customize the storefront and cater its functionalities to support bundled products.
Alternatively, you can build the storefront with your preferred tech stack.