|
1
|
--- |
|
2
|
sidebar_label: "Promotions Adjustments" |
|
3
|
products: |
|
4
|
- promotion |
|
5
|
--- |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Promotions Adjustments in Orders`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
In this document, you’ll learn how a promotion is applied to an order’s items and shipping methods using adjustment lines. |
|
14
|
|
|
15
|
## What are Adjustment Lines? |
|
16
|
|
|
17
|
An adjustment line indicates a change to a line item or a shipping method’s amount. It’s used to apply promotions or discounts on an order. |
|
18
|
|
|
19
|
The [OrderLineItemAdjustment data model](/references/order/models/OrderLineItemAdjustment) represents changes on a line item, and the [OrderShippingMethodAdjustment data model](/references/order/models/OrderShippingMethodAdjustment) represents changes on a shipping method. |
|
20
|
|
|
21
|
 |
|
22
|
|
|
23
|
The `amount` property of the adjustment line indicates the amount to be discounted from the original amount. |
|
24
|
|
|
25
|
<Note title="Tip"> |
|
26
|
|
|
27
|
The ID of the applied promotion is stored in the `promotion_id` property of the adjustment line. |
|
28
|
|
|
29
|
</Note> |
|
30
|
|
|
31
|
--- |
|
32
|
|
|
33
|
## Discountable Option |
|
34
|
|
|
35
|
The `OrderLineItem` data model has an `is_discountable` property that indicates whether promotions can be applied to the line item. It’s enabled by default. |
|
36
|
|
|
37
|
When disabled, a promotion can’t be applied to a line item. In the context of the Promotion Module, the promotion isn’t applied to the line item even if it matches its rules. |
|
38
|
|
|
39
|
|
|
40
|
--- |
|
41
|
|
|
42
|
## Promotion Actions |
|
43
|
|
|
44
|
When using the Order and Promotion modules together, use the [computeActions method of the Promotion Module’s main service](/references/promotion/computeActions). It retrieves the actions of line items and shipping methods. |
|
45
|
|
|
46
|
<Note> |
|
47
|
|
|
48
|
Learn more about actions in the [Promotion Module’s documentation](../../promotion/actions/page.mdx). |
|
49
|
|
|
50
|
</Note> |
|
51
|
|
|
52
|
```ts collapsibleLines="1-10" expandButtonLabel="Show Imports" |
|
53
|
import { |
|
54
|
ComputeActionAdjustmentLine, |
|
55
|
ComputeActionItemLine, |
|
56
|
ComputeActionShippingLine, |
|
57
|
// ... |
|
58
|
} from "@medusajs/framework/types" |
|
59
|
|
|
60
|
// ... |
|
61
|
|
|
62
|
// retrieve the order |
|
63
|
const order = await orderModuleService.retrieveOrder("ord_123", { |
|
64
|
relations: [ |
|
65
|
"items.item.adjustments", |
|
66
|
"shipping_methods.shipping_method.adjustments", |
|
67
|
], |
|
68
|
}) |
|
69
|
// retrieve the line item adjustments |
|
70
|
const lineItemAdjustments: ComputeActionItemLine[] = [] |
|
71
|
order.items.forEach((item) => { |
|
72
|
const filteredAdjustments = item.adjustments?.filter( |
|
73
|
(adjustment) => adjustment.code !== undefined |
|
74
|
) as unknown as ComputeActionAdjustmentLine[] |
|
75
|
if (filteredAdjustments.length) { |
|
76
|
lineItemAdjustments.push({ |
|
77
|
...item, |
|
78
|
...item.detail, |
|
79
|
adjustments: filteredAdjustments, |
|
80
|
}) |
|
81
|
} |
|
82
|
}) |
|
83
|
|
|
84
|
//retrieve shipping method adjustments |
|
85
|
const shippingMethodAdjustments: ComputeActionShippingLine[] = |
|
86
|
[] |
|
87
|
order.shipping_methods.forEach((shippingMethod) => { |
|
88
|
const filteredAdjustments = |
|
89
|
shippingMethod.adjustments?.filter( |
|
90
|
(adjustment) => adjustment.code !== undefined |
|
91
|
) as unknown as ComputeActionAdjustmentLine[] |
|
92
|
if (filteredAdjustments.length) { |
|
93
|
shippingMethodAdjustments.push({ |
|
94
|
...shippingMethod, |
|
95
|
adjustments: filteredAdjustments, |
|
96
|
}) |
|
97
|
} |
|
98
|
}) |
|
99
|
|
|
100
|
// compute actions |
|
101
|
const actions = await promotionModuleService.computeActions( |
|
102
|
["promo_123"], |
|
103
|
{ |
|
104
|
items: lineItemAdjustments, |
|
105
|
shipping_methods: shippingMethodAdjustments, |
|
106
|
// TODO infer from cart or region |
|
107
|
currency_code: "usd", |
|
108
|
} |
|
109
|
) |
|
110
|
``` |
|
111
|
|
|
112
|
The `computeActions` method accepts the existing adjustments of line items and shipping methods to compute the actions accurately. |
|
113
|
|
|
114
|
Then, use the returned `addItemAdjustment` and `addShippingMethodAdjustment` actions to set the order’s line items and the shipping method’s adjustments. |
|
115
|
|
|
116
|
```ts collapsibleLines="1-9" expandButtonLabel="Show Imports" |
|
117
|
import { |
|
118
|
AddItemAdjustmentAction, |
|
119
|
AddShippingMethodAdjustment, |
|
120
|
// ... |
|
121
|
} from "@medusajs/framework/types" |
|
122
|
|
|
123
|
// ... |
|
124
|
|
|
125
|
await orderModuleService.setOrderLineItemAdjustments( |
|
126
|
order.id, |
|
127
|
actions.filter( |
|
128
|
(action) => action.action === "addItemAdjustment" |
|
129
|
) as AddItemAdjustmentAction[] |
|
130
|
) |
|
131
|
|
|
132
|
await orderModuleService.setOrderShippingMethodAdjustments( |
|
133
|
order.id, |
|
134
|
actions.filter( |
|
135
|
(action) => |
|
136
|
action.action === "addShippingMethodAdjustment" |
|
137
|
) as AddShippingMethodAdjustment[] |
|
138
|
) |
|
139
|
``` |