Raw
1 ---
2 generate_toc: true
3 ---
4
5 import { CodeTabs, CodeTab } from "docs-ui"
6
7 export const metadata = {
8 title: `Cart Module`,
9 }
10
11 # {metadata.title}
12
13 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.
14
15 Medusa has cart related features available out-of-the-box through the Cart Module. A [module](!docs!/learn/fundamentals/modules) 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.
16
17 <Note>
18
19 Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation).
20
21 </Note>
22
23 ## Cart Features
24
25 - [Cart Management](./concepts/page.mdx): Store and manage carts, including their addresses, line items, shipping methods, and more.
26 - [Apply Promotion Adjustments](./promotions/page.mdx): Apply promotions or discounts to line items and shipping methods by adding adjustment lines that are factored into their subtotals.
27 - [Apply Tax Lines](./tax-lines/page.mdx): Apply tax lines to line items and shipping methods.
28 - [Cart Scoping](./links-to-other-modules/page.mdx): When used in the Medusa application, Medusa creates links to other Commerce Modules, scoping a cart to a sales channel, region, and a customer.
29
30 ---
31
32 ## How to Use the Cart Module
33
34 In your Medusa application, you build flows around Commerce Modules. A flow is built as a [Workflow](!docs!/learn/fundamentals/workflows), which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.
35
36 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.
37
38 For example:
39
40 export const highlights = [
41 ["12", "Modules.CART", "Resolve the module in a step."]
42 ]
43
44 ```ts title="src/workflows/create-cart.ts" highlights={highlights}
45 import {
46 createWorkflow,
47 WorkflowResponse,
48 createStep,
49 StepResponse,
50 } from "@medusajs/framework/workflows-sdk"
51 import { Modules } from "@medusajs/framework/utils"
52
53 const createCartStep = createStep(
54 "create-cart",
55 async ({}, { container }) => {
56 const cartModuleService = container.resolve(Modules.CART)
57
58 const cart = await cartModuleService.createCarts({
59 currency_code: "usd",
60 shipping_address: {
61 address_1: "1512 Barataria Blvd",
62 country_code: "us",
63 },
64 items: [
65 {
66 title: "Shirt",
67 unit_price: 10,
68 quantity: 1,
69 },
70 ],
71 })
72
73 return new StepResponse({ cart }, cart.id)
74 },
75 async (cartId, { container }) => {
76 if (!cartId) {
77 return
78 }
79 const cartModuleService = container.resolve(Modules.CART)
80
81 await cartModuleService.deleteCarts([cartId])
82 }
83 )
84
85 export const createCartWorkflow = createWorkflow(
86 "create-cart",
87 () => {
88 const { cart } = createCartStep()
89
90 return new WorkflowResponse({
91 cart,
92 })
93 }
94 )
95 ```
96
97 You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:
98
99 <CodeTabs group="resource-types">
100 <CodeTab label="API Route" value="api-route">
101
102 ```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
103 import type {
104 MedusaRequest,
105 MedusaResponse,
106 } from "@medusajs/framework/http"
107 import { createCartWorkflow } from "../../workflows/create-cart"
108
109 export async function GET(
110 req: MedusaRequest,
111 res: MedusaResponse
112 ) {
113 const { result } = await createCartWorkflow(req.scope)
114 .run()
115
116 res.send(result)
117 }
118 ```
119
120 </CodeTab>
121 <CodeTab label="Subscriber" value="subscriber">
122
123 ```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
124 import {
125 type SubscriberConfig,
126 type SubscriberArgs,
127 } from "@medusajs/framework"
128 import { createCartWorkflow } from "../workflows/create-cart"
129
130 export default async function handleUserCreated({
131 event: { data },
132 container,
133 }: SubscriberArgs<{ id: string }>) {
134 const { result } = await createCartWorkflow(container)
135 .run()
136
137 console.log(result)
138 }
139
140 export const config: SubscriberConfig = {
141 event: "user.created",
142 }
143 ```
144
145 </CodeTab>
146 <CodeTab label="Scheduled Job" value="scheduled-job">
147
148 ```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]}
149 import { MedusaContainer } from "@medusajs/framework/types"
150 import { createCartWorkflow } from "../workflows/create-cart"
151
152 export default async function myCustomJob(
153 container: MedusaContainer
154 ) {
155 const { result } = await createCartWorkflow(container)
156 .run()
157
158 console.log(result)
159 }
160
161 export const config = {
162 name: "run-once-a-day",
163 schedule: `0 0 * * *`,
164 }
165 ```
166
167 </CodeTab>
168 </CodeTabs>
169
170 Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows).
171
172 ---
173
174 <CommerceModuleSections />