|
1
|
export const metadata = { |
|
2
|
title: `Promotion Actions`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this document, you’ll learn about promotion actions and how they’re computed using the [computeActions method](/references/promotion/computeActions). |
|
8
|
|
|
9
|
## computeActions Method |
|
10
|
|
|
11
|
The Promotion Module's main service has a [computeActions method](/references/promotion/computeActions) that returns an array of actions to perform on a cart when one or more promotions are applied. |
|
12
|
|
|
13
|
Actions inform you what adjustment must be made to a cart item or shipping method. Each action is an object having the `action` property indicating the type of action. |
|
14
|
|
|
15
|
--- |
|
16
|
|
|
17
|
## How Promotions Stack |
|
18
|
|
|
19
|
When you pass more than one promotion to `computeActions`, Medusa applies them one after another and returns the combined actions. |
|
20
|
|
|
21
|
Medusa applies the promotions in the following order: |
|
22
|
|
|
23
|
1. `buyget` promotions, then `standard` promotions. |
|
24
|
2. Within each type, Medusa sorts promotions by their application method's `value` in descending order. |
|
25
|
|
|
26
|
Medusa computes each promotion against the remaining amount of a line item or shipping method: its original amount minus the amount that promotions earlier in the same computation already discounted. Medusa never discounts an item below `0`. |
|
27
|
|
|
28
|
This affects how percentages are calculated. A percentage promotion applies its percentage to the remaining amount, not the original amount. For example, if a `$30` discount already applied to a `$200` item, a following `10%` promotion discounts `10%` of the remaining `$170`, which is `$17`. |
|
29
|
|
|
30
|
<Note> |
|
31
|
|
|
32
|
Medusa tracks the remaining amount within a single `computeActions` call. Across separate calls, Medusa reflects previously applied adjustments in the item's amount. |
|
33
|
|
|
34
|
</Note> |
|
35
|
|
|
36
|
### Order-Level Promotions |
|
37
|
|
|
38
|
Medusa always distributes a promotion whose application method's `target_type` is `order` across the cart's applicable items, whether its value is a percentage or a fixed amount. Medusa distributes the discount proportionally based on each item's remaining amount. |
|
39
|
|
|
40
|
For example, consider a cart with two items: |
|
41
|
|
|
42
|
- Item A: `$250` |
|
43
|
- Item B: `$150` |
|
44
|
|
|
45
|
If you apply a `$100` fixed discount on the order, Medusa distributes it proportionally across the two items based on their share of the `$400` total: |
|
46
|
|
|
47
|
- Item A: `$100 x (250 / 400) = $62.5` |
|
48
|
- Item B: `$100 x (150 / 400) = $37.5` |
|
49
|
|
|
50
|
If another order-level promotion applies afterward, Medusa recalculates each item's share using its remaining amount (`$187.5` and `$112.5`). |
|
51
|
|
|
52
|
--- |
|
53
|
|
|
54
|
## Action Types |
|
55
|
|
|
56
|
### `addItemAdjustment` Action |
|
57
|
|
|
58
|
The `addItemAdjustment` action indicates that an adjustment must be made to an item. For example, removing $5 off its amount. |
|
59
|
|
|
60
|
This action has the following format: |
|
61
|
|
|
62
|
```ts |
|
63
|
export interface AddItemAdjustmentAction { |
|
64
|
action: "addItemAdjustment" |
|
65
|
item_id: string |
|
66
|
amount: number |
|
67
|
code: string |
|
68
|
description?: string |
|
69
|
is_tax_inclusive?: boolean |
|
70
|
} |
|
71
|
``` |
|
72
|
|
|
73
|
This action means that a new record should be created of the `LineItemAdjustment` data model in the Cart Module, or `OrderLineItemAdjustment` data model in the Order Module. |
|
74
|
|
|
75
|
<Note> |
|
76
|
|
|
77
|
Refer to [this reference](/references/promotion/interfaces/promotion.AddItemAdjustmentAction) for details on the object’s properties. |
|
78
|
|
|
79
|
</Note> |
|
80
|
|
|
81
|
### `removeItemAdjustment` Action |
|
82
|
|
|
83
|
The `removeItemAdjustment` action indicates that an adjustment must be removed from a line item. For example, remove the $5 discount. |
|
84
|
|
|
85
|
The `computeActions` method accepts any previous item adjustments in the `items` property of the second parameter. |
|
86
|
|
|
87
|
This action has the following format: |
|
88
|
|
|
89
|
```ts |
|
90
|
export interface RemoveItemAdjustmentAction { |
|
91
|
action: "removeItemAdjustment" |
|
92
|
adjustment_id: string |
|
93
|
description?: string |
|
94
|
code: string |
|
95
|
} |
|
96
|
``` |
|
97
|
|
|
98
|
This action means that a new record should be removed of the `LineItemAdjustment` (or `OrderLineItemAdjustment`) with the specified ID in the `adjustment_id` property. |
|
99
|
|
|
100
|
<Note> |
|
101
|
|
|
102
|
Refer to [this reference](/references/promotion/interfaces/promotion.RemoveItemAdjustmentAction) for details on the object’s properties. |
|
103
|
|
|
104
|
</Note> |
|
105
|
|
|
106
|
### `addShippingMethodAdjustment` Action |
|
107
|
|
|
108
|
The `addShippingMethodAdjustment` action indicates that an adjustment must be made on a shipping method. For example, make the shipping method free. |
|
109
|
|
|
110
|
This action has the following format: |
|
111
|
|
|
112
|
```ts |
|
113
|
export interface AddShippingMethodAdjustment { |
|
114
|
action: "addShippingMethodAdjustment" |
|
115
|
shipping_method_id: string |
|
116
|
amount: number |
|
117
|
code: string |
|
118
|
description?: string |
|
119
|
} |
|
120
|
``` |
|
121
|
|
|
122
|
This action means that a new record should be created of the `ShippingMethodAdjustment` data model in the Cart Module, or `OrderShippingMethodAdjustment` data model in the Order Module. |
|
123
|
|
|
124
|
<Note> |
|
125
|
|
|
126
|
Refer to [this reference](/references/promotion/interfaces/promotion.AddShippingMethodAdjustment) for details on the object’s properties. |
|
127
|
|
|
128
|
</Note> |
|
129
|
|
|
130
|
### `removeShippingMethodAdjustment` Action |
|
131
|
|
|
132
|
The `removeShippingMethodAdjustment` action indicates that an adjustment must be removed from a shipping method. For example, remove the free shipping discount. |
|
133
|
|
|
134
|
The `computeActions` method accepts any previous shipping method adjustments in the `shipping_methods` property of the second parameter. |
|
135
|
|
|
136
|
This action has the following format: |
|
137
|
|
|
138
|
```ts |
|
139
|
export interface RemoveShippingMethodAdjustment { |
|
140
|
action: "removeShippingMethodAdjustment" |
|
141
|
adjustment_id: string |
|
142
|
code: string |
|
143
|
} |
|
144
|
``` |
|
145
|
|
|
146
|
When the Medusa application receives this action type, it removes the `ShippingMethodAdjustment` (or `OrderShippingMethodAdjustment`) with the specified ID in the `adjustment_id` property. |
|
147
|
|
|
148
|
<Note> |
|
149
|
|
|
150
|
Refer to [this reference](/references/promotion/interfaces/promotion.RemoveShippingMethodAdjustment) for details on the object’s properties. |
|
151
|
|
|
152
|
</Note> |
|
153
|
|
|
154
|
### `campaignBudgetExceeded` Action |
|
155
|
|
|
156
|
When the `campaignBudgetExceeded` action is returned, the promotions within a campaign can no longer be used as the campaign budget has been exceeded. |
|
157
|
|
|
158
|
This action has the following format: |
|
159
|
|
|
160
|
```ts |
|
161
|
export interface CampaignBudgetExceededAction { |
|
162
|
action: "campaignBudgetExceeded" |
|
163
|
code: string |
|
164
|
} |
|
165
|
``` |
|
166
|
|
|
167
|
<Note> |
|
168
|
|
|
169
|
Refer to [this reference](/references/promotion/interfaces/promotion.CampaignBudgetExceededAction) for details on the object’s properties. |
|
170
|
|
|
171
|
</Note> |