1 import { Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `Draft Orders Plugin`,
5 }
6
7 # {metadata.title}
8
9 In this guide, you'll learn about the Draft Orders Plugin and its features.
10
11 <Note>
12
13 The Draft Orders Plugin was initially only available to Cloud users during its early access phase. It is now available for all Medusa users.
14
15 </Note>
16
17 ## What is the Draft Orders Plugin?
18
19 The Draft Orders Plugin is a Medusa plugin that allows admin users to create and manage draft orders on behalf of customers from the Medusa Admin.
20
21 The Medusa application already has the foundation to support draft orders, including data models and API routes. This plugin adds a user interface for managing draft orders in the Medusa Admin dashboard.
22
23 The Draft Orders Plugin is especially useful for handling customer support scenarios or when a customer places an order offline, such as over the phone or in-store.
24
25 ### Features
26
27 - [Create draft orders from the Medusa Admin.](!user-guide!/orders/draft-orders/create)
28 - [Manage items in a draft order, allowing admin users to add, update, or remove items](!user-guide!/orders/draft-orders/manage#manage-draft-orders-items).
29 - [Add shipping methods to draft orders.](!user-guide!/orders/draft-orders/manage#manage-draft-orders-shipping-methods).
30 - [Associate existing customers with draft orders.](!user-guide!/orders/draft-orders/manage#manage-draft-orders-customer-details).
31 - [Convert draft orders to regular orders.](!user-guide!/orders/draft-orders/manage#convert-draft-order-to-regular-order).
32
33 ---
34
35 ## Install the Draft Orders Plugin
36
37 The Draft Orders Plugin is available in all Medusa applications starting v2.10.0.
38
39 For earlier versions, you can install the plugin manually.
40
41 <Prerequisites
42 items={[
43 {
44 text: "Medusa application >= v2.4.0",
45 link: "!docs!/learn/installation"
46 }
47 ]}
48 />
49
50 To install the Draft Orders Plugin:
51
52 1. Run the following command in your Medusa application's directory:
53
54 ```bash npm2yarn
55 npm install @medusajs/draft-order
56 ```
57
58 2. Add the plugin to your `medusa-config.ts` file:
59
60 ```ts title="medusa-config.ts"
61 module.exports = defineConfig({
62 // ...
63 plugins: [
64 {
65 resolve: "@medusajs/draft-order",
66 options: {},
67 },
68 ],
69 })
70 ```
71
72 ### Test the Draft Orders Plugin
73
74 To test the Draft Orders Plugin, start the Medusa application with the following command:
75
76 ```bash npm2yarn
77 npm run dev
78 ```
79
80 If you open the Medusa Admin at `localhost:9000/app` and log in, you'll find an "Orders -> Drafts" sidebar item.
81
82 ---
83
84 ## Draft Orders User Guides
85
86 To learn how to use the draft order features in the Medusa Admin, refer to the [Draft Orders](!user-guide!/orders/draft-orders) user guides.
87
88 ---
89
90 ## How Draft Orders Work
91
92 A draft order is stored in the database as a regular order. It is represented by the [Order data model](/references/order/models/Order) with the following properties:
93
94 - `status`: Set to `draft` to indicate that the order is a draft.
95 - `is_draft_order`: Set to `true` to indicate that the order is a draft order.
96
97 So, the same [order concepts](../concepts/page.mdx) apply to draft orders as well.
98
99 ### Editing Draft Orders
100
101 Similar to regular orders, draft orders can be [edited](../edit/page.mdx), allowing admin users to add, update, or remove items, as well as add shipping methods.
102
103 When the order edit is confirmed on the draft order, the changes are applied directly to the draft order and its [version](../order-versioning/page.mdx) is incremented.
104
105 ### Calculated Shipping Options
106
107 <Note>
108
109 This behavior is available since Medusa [v2.20.0](https://github.com/medusajs/medusa/releases/tag/v2.20.0).
110
111 </Note>
112
113 Draft orders support calculated shipping options, in addition to flat-rate options. When you add a calculated shipping method to a draft order edit using `addDraftOrderShippingMethodsWorkflow`, Medusa calls the fulfillment provider to price the method against the order's projected items.
114
115 Medusa automatically keeps calculated shipping prices up to date during the edit:
116
117 - When items are added or removed, Medusa reprices any pending calculated shipping methods against the updated projected items.
118 - When the edit is confirmed, Medusa reprices any already-applied calculated shipping methods against the final materialized order items.
119
120 Shipping methods with a custom amount (`is_custom_amount: true`) and flat-rate methods are never repriced automatically.
121
122 The `fetchShippingOptionForDraftOrderWorkflow` exposes two hooks you can use to inject additional context before the fulfillment provider calculates the price:
123
124 - `setPricingContext`: inject additional pricing context (for example, a `location_id`) used when resolving flat-rate prices from the Pricing Module. The context is merged with the order's `currency_code`.
125 - `setCalculatedShippingPricingContext`: inject custom context that is forwarded to the fulfillment provider's `calculatePrice` method when pricing a calculated shipping option. The returned object is merged into the `context` parameter of `calculatePrice`; framework-provided properties take precedence on any naming conflict.
126
127 The `refreshConfirmedDraftOrderShippingMethodsWorkflow` and `refreshPendingDraftOrderShippingMethodsWorkflow` workflows also expose a `setCalculatedShippingPricingContext` hook for the same purpose when repricing already-applied or pending calculated shipping methods.
128
129 ### Draft Order Locale
130
131 A draft order's `locale` can be set and updated before converting it to a regular order. All item details in the draft order, such as product titles and descriptions, will be presented in the correct language if translations are available.
132
133 When the draft order is converted to a regular order, it retains its `locale` property. The details of the order items will remain in the specified locale.
134
135 ### Tax Lines and Address Changes
136
137 <Note>
138
139 This behavior is available since Medusa [v2.19.0](https://github.com/medusajs/medusa/releases/tag/v2.19.0).
140
141 </Note>
142
143 When you update a draft order's shipping or billing address using the `updateDraftOrderWorkflow`, Medusa automatically recomputes the draft order's tax lines. This ensures the tax lines reflect the correct tax jurisdiction after the address change.
144
145 Whether Medusa recomputes the tax lines depends on the region's `automatic_taxes` setting:
146
147 - If `automatic_taxes` is enabled, Medusa recomputes the tax lines from the tax provider.
148 - If `automatic_taxes` is disabled, the tax lines are not recomputed.
149
150 This behavior is different from regular orders: updating a regular order's address using the `updateOrderWorkflow` does not trigger a tax recomputation.
151
152 ### Converting Draft Orders to Regular Orders
153
154 Once a draft order is finalized and ready for processing, it can be converted to a regular order. This involves:
155
156 - Changing its `status` property to `pending`.
157 - Changing its `is_draft_order` property to `false`.
158
159 When converting a draft order using the `convertDraftOrderWorkflow`, Medusa recomputes the draft order's tax lines before placing the order. This ensures the order never carries stale tax lines, for example from a previous address change. The recomputation also respects the region's `automatic_taxes` setting.
160
161 Admin users can then manage the order like any other regular order, including processing payments and fulfilling items.