1 ---
2 generate_toc: true
3 ---
4
5 import { CodeTabs, CodeTab } from "docs-ui"
6
7 export const metadata = {
8 title: `Order Module`,
9 }
10
11 # {metadata.title}
12
13 In this section of the documentation, you will find resources to learn more about the Order Module and how to use it in your application.
14
15 <Note title="Looking for no-code docs?">
16
17 Refer to the [Medusa Admin User Guide](!user-guide!/orders) to learn how to manage orders using the dashboard.
18
19 </Note>
20
21 Medusa has order related features available out-of-the-box through the Order 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 Order Module.
22
23 <Note>
24
25 Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation).
26
27 </Note>
28
29 ## Order Features
30
31 - [Order Management](./concepts/page.mdx): Store and manage your orders to retrieve, create, cancel, and perform other operations.
32 - [Draft Orders](./draft-orders/page.mdx): Allow merchants to create orders on behalf of their customers as draft orders that later are transformed to regular orders.
33 - [Apply Promotion Adjustments](./promotion-adjustments/page.mdx): Apply promotions or discounts to the order's items and shipping methods by adding adjustment lines that are factored into their subtotals.
34 - [Apply Tax Lines](./tax-lines/page.mdx): Apply tax lines to an order's line items and shipping methods.
35 - [Returns](./return/page.mdx), [Edits](./edit/page.mdx), [Exchanges](./exchange/page.mdx), and [Claims](./claim/page.mdx): Make [changes](./order-change/page.mdx) to an order to edit, return, or exchange its items, with [version-based control](./order-versioning/page.mdx) over the order's timeline.
36
37 ---
38
39 ## How to Use the Order Module
40
41 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.
42
43 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.
44
45 For example:
46
47 export const highlights = [
48 ["12", "Modules.ORDER", "Resolve the module in a step."]
49 ]
50
51 ```ts title="src/workflows/create-draft-order.ts" highlights={highlights}
52 import {
53 createWorkflow,
54 WorkflowResponse,
55 createStep,
56 StepResponse,
57 } from "@medusajs/framework/workflows-sdk"
58 import { Modules } from "@medusajs/framework/utils"
59
60 const createDraftOrderStep = createStep(
61 "create-order",
62 async ({}, { container }) => {
63 const orderModuleService = container.resolve(Modules.ORDER)
64
65 const draftOrder = await orderModuleService.createOrders({
66 currency_code: "usd",
67 items: [
68 {
69 title: "Shirt",
70 quantity: 1,
71 unit_price: 30,
72 },
73 ],
74 shipping_methods: [
75 {
76 name: "Express shipping",
77 amount: 30,
78 },
79 ],
80 status: "draft",
81 })
82
83 return new StepResponse({ draftOrder }, draftOrder.id)
84 },
85 async (draftOrderId, { container }) => {
86 if (!draftOrderId) {
87 return
88 }
89 const orderModuleService = container.resolve(Modules.ORDER)
90
91 await orderModuleService.deleteOrders([draftOrderId])
92 }
93 )
94
95 export const createDraftOrderWorkflow = createWorkflow(
96 "create-draft-order",
97 () => {
98 const { draftOrder } = createDraftOrderStep()
99
100 return new WorkflowResponse({
101 draftOrder,
102 })
103 }
104 )
105 ```
106
107 You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:
108
109 <CodeTabs group="resource-types">
110 <CodeTab label="API Route" value="api-route">
111
112 ```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
113 import type {
114 MedusaRequest,
115 MedusaResponse,
116 } from "@medusajs/framework/http"
117 import { createDraftOrderWorkflow } from "../../workflows/create-draft-order"
118
119 export async function GET(
120 req: MedusaRequest,
121 res: MedusaResponse
122 ) {
123 const { result } = await createDraftOrderWorkflow(req.scope)
124 .run()
125
126 res.send(result)
127 }
128 ```
129
130 </CodeTab>
131 <CodeTab label="Subscriber" value="subscriber">
132
133 ```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
134 import {
135 type SubscriberConfig,
136 type SubscriberArgs,
137 } from "@medusajs/framework"
138 import { createDraftOrderWorkflow } from "../workflows/create-draft-order"
139
140 export default async function handleUserCreated({
141 event: { data },
142 container,
143 }: SubscriberArgs<{ id: string }>) {
144 const { result } = await createDraftOrderWorkflow(container)
145 .run()
146
147 console.log(result)
148 }
149
150 export const config: SubscriberConfig = {
151 event: "user.created",
152 }
153 ```
154
155 </CodeTab>
156 <CodeTab label="Scheduled Job" value="scheduled-job">
157
158 ```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]}
159 import { MedusaContainer } from "@medusajs/framework/types"
160 import { createDraftOrderWorkflow } from "../workflows/create-draft-order"
161
162 export default async function myCustomJob(
163 container: MedusaContainer
164 ) {
165 const { result } = await createDraftOrderWorkflow(container)
166 .run()
167
168 console.log(result)
169 }
170
171 export const config = {
172 name: "run-once-a-day",
173 schedule: `0 0 * * *`,
174 }
175 ```
176
177 </CodeTab>
178 </CodeTabs>
179
180 Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows).
181
182 ---
183
184 <CommerceModuleSections />