|
1
|
export const metadata = { |
|
2
|
title: `Order Versioning`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
|
|
8
|
In this document, you’ll learn how orders and their details are versioned. |
|
9
|
|
|
10
|
## What is Order Versioning? |
|
11
|
|
|
12
|
Versioning means assigning a version number to a record, such as an order and its items. This is useful for viewing the different versions of an order following changes throughout its lifetime. |
|
13
|
|
|
14
|
When changes are confirmed on an order, such as when an item is added or returned, the order's version changes. |
|
15
|
|
|
16
|
--- |
|
17
|
|
|
18
|
## version Property |
|
19
|
|
|
20
|
The `Order` and `OrderSummary` data models have a `version` property that indicates the current version. By default, its value is `1`. |
|
21
|
|
|
22
|
Other order-related data models, such as `OrderItem`, also have a `version` property, but it indicates the version to which the record belongs. |
|
23
|
|
|
24
|
<Note> |
|
25
|
|
|
26
|
Since [Medusa v2.14.0](https://github.com/medusajs/medusa/releases/tag/v2.14.0), adjustment data models like `OrderLineItemAdjustment` and `OrderShippingMethodAdjustment` also include a `version` property to track which version they belong to. |
|
27
|
|
|
28
|
</Note> |
|
29
|
|
|
30
|
--- |
|
31
|
|
|
32
|
## How the Version Changes |
|
33
|
|
|
34
|
When the order is changed, such as when an item is exchanged, Medusa updates the order's version and its related data: |
|
35
|
|
|
36
|
1. The version of the order and its summary is incremented. |
|
37
|
2. Related order data that have a `version` property, such as `OrderItem`, are duplicated. The duplicated item has the new version, while the original item retains the previous version. |
|
38
|
|
|
39
|
When the order is retrieved, only the related data with the same version is returned. |
|
40
|
|
|
41
|
--- |
|
42
|
|
|
43
|
## Retrieve Versioned Item Details |
|
44
|
|
|
45
|
An order's item is stored across two data models: |
|
46
|
|
|
47
|
- The [OrderLineItem data model](/references/order/models/OrderLineItem) holds the item's original details, such as its title and the price at the time of purchase. This record is not versioned. |
|
48
|
- The [OrderItem data model](/references/order/models/OrderItem) links the line item to an order version. It holds the versioned `quantity`, `unit_price`, `compare_at_unit_price`, and `metadata` of the item in that version. |
|
49
|
|
|
50
|
When an [order edit](../edit/page.mdx), claim, exchange, or return changes an item's quantity, price, or metadata, the Order Module writes the new values to the `OrderItem` record of the new version. The `OrderLineItem` record keeps its original values. |
|
51
|
|
|
52
|
### Retrieved Item Shape |
|
53
|
|
|
54
|
When you retrieve an order, the Order Module merges both records into each item of the `items` property. The item's `quantity`, `unit_price`, `compare_at_unit_price`, and `metadata` hold the values of the order's current version, and the remaining properties come from the line item. |
|
55
|
|
|
56
|
The merged item also has the following properties: |
|
57
|
|
|
58
|
- `detail`: the `OrderItem` record of the current version. Use properties like `detail.unit_price` and `detail.metadata` to read the versioned values directly. |
|
59
|
- `line_item_metadata`: the `metadata` of the `OrderLineItem` record, which is the metadata set when the item was first added to the order. |
|
60
|
|
|
61
|
For example, retrieve an order's versioned item details using [Query](!docs!/learn/fundamentals/query): |
|
62
|
|
|
63
|
```ts |
|
64
|
const { data: orders } = await query.graph({ |
|
65
|
entity: "order", |
|
66
|
fields: [ |
|
67
|
"items.unit_price", |
|
68
|
"items.metadata", |
|
69
|
"items.detail.unit_price", |
|
70
|
"items.detail.metadata", |
|
71
|
"items.line_item_metadata", |
|
72
|
], |
|
73
|
filters: { |
|
74
|
id: orderId, |
|
75
|
}, |
|
76
|
}) |
|
77
|
``` |
|
78
|
|
|
79
|
<Note> |
|
80
|
|
|
81
|
The `items.unit_price` and `items.metadata` fields already reflect the order's current version. Select `items.detail.*` when you want the versioned values without the merge, or `items.line_item_metadata` when you want the metadata set at purchase time. |
|
82
|
|
|
83
|
</Note> |