Cart Module

In this section of the documentation, you will find resources to learn more about the Cart Module and how to use it in your application.

Medusa has cart related features available out-of-the-box through the Cart Module. A module is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in commerce modules, such as this Cart Module.

NoteLearn more about why modules are isolated in this documentation.

Cart Features#

  • Cart Management: Store and manage carts, including their addresses, line items, shipping methods, and more.
  • Apply Promotion Adjustments: Apply promotions or discounts to line items and shipping methods by adding adjustment lines that are factored into their subtotals.
  • Apply Tax Lines: Apply tax lines to line items and shipping methods.
  • Cart Scoping: When used in the Medusa application, Medusa creates links to other commerce modules, scoping a cart to a sales channel, region, and a customer.

How to Use the Cart Module#

In your Medusa application, you build flows around commerce modules. A flow is built as a Workflow, which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.

You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the @medusajs/medusa/core-flows package.

For example:

src/workflows/create-cart.ts
1import { 2  createWorkflow, 3  WorkflowResponse,4  createStep,5  StepResponse,6} from "@medusajs/framework/workflows-sdk"7import { Modules } from "@medusajs/framework/utils"8
9const createCartStep = createStep(10  "create-cart",11  async ({}, { container }) => {12    const cartModuleService = container.resolve(Modules.CART)13
14    const cart = await cartModuleService.createCarts({15      currency_code: "usd",16      shipping_address: {17        address_1: "1512 Barataria Blvd",18        country_code: "us",19      },20      items: [21        {22          title: "Shirt",23          unit_price: 1000,24          quantity: 1,25        },26      ],27    })28
29    return new StepResponse({ cart }, cart.id)30  },31  async (cartId, { container }) => {32    if (!cartId) {33      return34    }35    const cartModuleService = container.resolve(Modules.CART)36
37    await cartModuleService.deleteCarts([cartId])38  }39)40
41export const createCartWorkflow = createWorkflow(42  "create-cart",43  () => {44    const { cart } = createCartStep()45
46    return new WorkflowResponse({47      cart,48    })49  }50)

You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:

src/api/workflow/route.ts
5import { createCartWorkflow } from "../../workflows/create-cart"6
7export async function GET(8  req: MedusaRequest,9  res: MedusaResponse10) {11  const { result } = await createCartWorkflow(req.scope)12    .run()13
14  res.send(result)15}

Learn more about workflows in this documentation.


Server Guides#

Learn how to use the Cart Module in your customizations on the Medusa application server.

Extend Module
Implement Custom Line Item Pricing in Medusa

Storefront Guides#

Learn how to integrate the Cart Module's features into your storefront.

Create Cart Context in Storefront
Create Cart in Storefront
Manage Cart's Items in Storefront
Retrieve Cart in Storefront
Update Cart in Storefront
Checkout Step 2: Set Address
Checkout Step 5: Complete Cart
Checkout Step 1: Enter Email
Checkout Step 4: Choose Payment Provider
Payment with Stripe in React Storefront
Checkout Step 3: Choose Shipping Method

References#

Find references for tools and resources related to the Cart Module, such as data models, methods, and more. These are useful for your customizations.

Workflows
JS SDK
Events Reference
Main Service Reference
Data Models Reference
Was this page helpful?
Edited Jan 9·Edit this page
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break