|
1
|
export const metadata = { |
|
2
|
title: `Custom Order Display ID`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this guide, you'll learn how to customize the display ID of orders in Medusa. |
|
8
|
|
|
9
|
<Note> |
|
10
|
|
|
11
|
This feature is available since [Medusa v2.12.0](https://github.com/medusajs/medusa/releases/tag/v2.12.0). |
|
12
|
|
|
13
|
</Note> |
|
14
|
|
|
15
|
## Default Display ID |
|
16
|
|
|
17
|
By default, Medusa stores the display ID of orders in the `display_id` property of the [Order data model](/references/order/models/Order). The display ID is a serial integer that starts at 1 and increments with each new order. |
|
18
|
|
|
19
|
For example: |
|
20
|
|
|
21
|
```json |
|
22
|
{ |
|
23
|
"id": "order_123", |
|
24
|
"display_id": 1, |
|
25
|
// other properties... |
|
26
|
} |
|
27
|
``` |
|
28
|
|
|
29
|
--- |
|
30
|
|
|
31
|
## Custom Display ID |
|
32
|
|
|
33
|
In some cases, you might want to use a custom display ID for orders. This is useful for integrating with external systems or providing a more user-friendly order identifier. |
|
34
|
|
|
35
|
The `Order` data model has a `custom_display_id` property that stores a custom display ID you generate. |
|
36
|
|
|
37
|
You can define the logic for generating this ID in the `generateCustomDisplayId` module option set in `medusa-config.ts`. |
|
38
|
|
|
39
|
For example: |
|
40
|
|
|
41
|
```ts title="medusa-config.ts" |
|
42
|
// other imports... |
|
43
|
import { Modules } from "@medusajs/framework/utils" |
|
44
|
import { OrderTypes, Context } from "@medusajs/framework/types" |
|
45
|
|
|
46
|
module.exports = defineConfig({ |
|
47
|
modules: [ |
|
48
|
{ |
|
49
|
key: Modules.ORDER, |
|
50
|
options: { |
|
51
|
generateCustomDisplayId: async function ( |
|
52
|
order: OrderTypes.CreateOrderDTO, |
|
53
|
sharedContext: Context |
|
54
|
): Promise<string> { |
|
55
|
// Return your custom display ID |
|
56
|
return `${order.email}-${Date.now()}` |
|
57
|
}, |
|
58
|
}, |
|
59
|
}, |
|
60
|
// other modules... |
|
61
|
], |
|
62
|
// other configurations... |
|
63
|
}) |
|
64
|
``` |
|
65
|
|
|
66
|
In the example above, the `generateCustomDisplayId` function generates a custom display ID by combining the order's email with the current timestamp. |
|
67
|
|
|
68
|
You can implement any logic to generate a unique and meaningful display ID for your orders. |
|
69
|
|
|
70
|
--- |
|
71
|
|
|
72
|
## View Custom Display ID in Medusa Admin |
|
73
|
|
|
74
|
By default, Medusa Admin displays the `display_id` in the table on the Orders page. To view the custom display ID in the table, you can enable the `view_configurations` experimental feature. |
|
75
|
|
|
76
|
To enable this feature, add the following to `medusa-config.ts`: |
|
77
|
|
|
78
|
```ts title="medusa-config.ts" |
|
79
|
module.exports = defineConfig({ |
|
80
|
// other configurations... |
|
81
|
featureFlags: { |
|
82
|
view_configurations: true, |
|
83
|
}, |
|
84
|
}) |
|
85
|
``` |
|
86
|
|
|
87
|
This enables the feature's flag. |
|
88
|
|
|
89
|
Next, run the necessary migrations: |
|
90
|
|
|
91
|
```bash npx2yarn |
|
92
|
npx medusa db:migrate |
|
93
|
``` |
|
94
|
|
|
95
|
Then, start the Medusa application: |
|
96
|
|
|
97
|
```bash npm2yarn |
|
98
|
npm run dev |
|
99
|
``` |
|
100
|
|
|
101
|
Finally, customize the Order view in Medusa Admin to display the `custom_display_id` property. |
|
102
|
|
|
103
|
--- |
|
104
|
|
|
105
|
## Search Orders by Display ID |
|
106
|
|
|
107
|
The `display_id` and `custom_display_id` properties are searchable fields of the `Order` data model. So, you can search orders by either display ID without any customizations. |
|
108
|
|
|
109
|
The Order Module also defines database indexes on both properties. The index on `custom_display_id` is unique, so each custom display ID must be unique among orders that aren't deleted. |
|
110
|
|
|
111
|
### Search in Medusa Admin |
|
112
|
|
|
113
|
The search box on the Orders page of Medusa Admin searches the order's searchable fields, including `display_id` and `custom_display_id`. So, you can enter an order's display ID or custom display ID in the search box to find it. |
|
114
|
|
|
115
|
### Search with the Admin API |
|
116
|
|
|
117
|
To search orders by display ID with the [List Orders API route](!api!/admin/orders/list-orders), pass the display ID or custom display ID in the `q` query parameter. |
|
118
|
|
|
119
|
For example, using the [JS SDK](../../../js-sdk/page.mdx): |
|
120
|
|
|
121
|
```ts |
|
122
|
const { orders } = await sdk.admin.order.list({ |
|
123
|
q: "ORD-1234", |
|
124
|
}) |
|
125
|
``` |
|
126
|
|
|
127
|
The `q` parameter performs a case-insensitive partial match on the order's searchable fields, which also include the email and the shipping and billing addresses. For example, searching for `12` returns orders whose display ID is `12`, `120`, or `1234`. |
|
128
|
|
|
129
|
<Note> |
|
130
|
|
|
131
|
The List Orders API route doesn't accept `display_id` or `custom_display_id` as dedicated filter parameters. Use the `q` parameter instead. |
|
132
|
|
|
133
|
</Note> |
|
134
|
|
|
135
|
### Filter with Query in Customizations |
|
136
|
|
|
137
|
To retrieve an order by its exact display ID or custom display ID in your customizations, such as an API route or workflow step, use [Query](!docs!/learn/fundamentals/query) and pass the property in `filters`. |
|
138
|
|
|
139
|
For example: |
|
140
|
|
|
141
|
```ts |
|
142
|
const { data: orders } = await query.graph({ |
|
143
|
entity: "order", |
|
144
|
fields: ["id", "display_id", "custom_display_id"], |
|
145
|
filters: { |
|
146
|
custom_display_id: "ORD-1234", |
|
147
|
}, |
|
148
|
}) |
|
149
|
``` |