|
1
|
import { Table } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Promotion Concepts`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this guide, you’ll learn about the main promotion and rule concepts in the Promotion Module. |
|
10
|
|
|
11
|
<Note title="Looking for no-code docs?"> |
|
12
|
|
|
13
|
Refer to this [Medusa Admin User Guide](!user-guide!/promotions) to learn how to manage promotions using the dashboard. |
|
14
|
|
|
15
|
</Note> |
|
16
|
|
|
17
|
## What is a Promotion? |
|
18
|
|
|
19
|
A promotion, represented by the [Promotion data model](/references/promotion/models/Promotion), is a discount that can be applied on cart items, shipping methods, or entire orders. |
|
20
|
|
|
21
|
A promotion has two types: |
|
22
|
|
|
23
|
- `standard`: A standard promotion with rules. |
|
24
|
- `buyget`: “A buy X get Y” promotion with rules. |
|
25
|
|
|
26
|
<Table> |
|
27
|
<Table.Header> |
|
28
|
<Table.Row> |
|
29
|
<Table.HeaderCell> |
|
30
|
|
|
31
|
`standard` Promotion Examples |
|
32
|
|
|
33
|
</Table.HeaderCell> |
|
34
|
<Table.HeaderCell> |
|
35
|
|
|
36
|
`buyget` Promotion Examples |
|
37
|
|
|
38
|
</Table.HeaderCell> |
|
39
|
</Table.Row> |
|
40
|
</Table.Header> |
|
41
|
<Table.Body> |
|
42
|
<Table.Row> |
|
43
|
<Table.Cell> |
|
44
|
|
|
45
|
A coupon code that gives customers 10% off their entire order. |
|
46
|
|
|
47
|
</Table.Cell> |
|
48
|
<Table.Cell> |
|
49
|
|
|
50
|
Buy two shirts and get another for free. |
|
51
|
|
|
52
|
</Table.Cell> |
|
53
|
</Table.Row> |
|
54
|
<Table.Row> |
|
55
|
<Table.Cell> |
|
56
|
|
|
57
|
A coupon code that gives customers $15 off any shirt in their order. |
|
58
|
|
|
59
|
</Table.Cell> |
|
60
|
<Table.Cell> |
|
61
|
|
|
62
|
Buy two shirts and get 10% off the entire order. |
|
63
|
|
|
64
|
</Table.Cell> |
|
65
|
</Table.Row> |
|
66
|
<Table.Row> |
|
67
|
<Table.Cell> |
|
68
|
|
|
69
|
A discount applied automatically for VIP customers that removes 10% off their shipping method’s amount. |
|
70
|
|
|
71
|
</Table.Cell> |
|
72
|
<Table.Cell> |
|
73
|
|
|
74
|
Spend $100 and get free shipping. |
|
75
|
|
|
76
|
</Table.Cell> |
|
77
|
</Table.Row> |
|
78
|
</Table.Body> |
|
79
|
</Table> |
|
80
|
|
|
81
|
<Note> |
|
82
|
|
|
83
|
The Medusa Admin UI may not provide a way to create each of these promotion examples. However, they are supported by the Promotion Module and Medusa's workflows and API routes. |
|
84
|
|
|
85
|
</Note> |
|
86
|
|
|
87
|
--- |
|
88
|
|
|
89
|
## Promotion Limits |
|
90
|
|
|
91
|
<Note> |
|
92
|
|
|
93
|
The `limit` property is available since [Medusa v2.12.0](https://github.com/medusajs/medusa/releases/tag/v2.12.0). |
|
94
|
|
|
95
|
</Note> |
|
96
|
|
|
97
|
A promotion can have usage limits to restrict how many times it can be used. |
|
98
|
|
|
99
|
There are three ways to limit a promotion's usage: |
|
100
|
|
|
101
|
1. By setting its `limit` property: This limits the total number of times the promotion can be used across all orders. |
|
102
|
2. By setting the [global budget on the promotion's campaign](../campaign/page.mdx#global-budgets): This limits the total spend or usage across all promotions in the campaign. |
|
103
|
3. By setting the [attribute-based budget on the promotion's campaign](../campaign/page.mdx#attribute-based-budgets): This limits the total spend or usage for specific attributes across all promotions in the campaign. For example, limiting the budget for a specific customer group. |
|
104
|
|
|
105
|
All budgets are applied on the promotion. Once a budget is exhausted, the promotion can no longer be applied. |
|
106
|
|
|
107
|
For example, if a promotion has a `limit` of `10` and its campaign has a global budget of `$1000`, the promotion can only be used `10` times or until the total discount given reaches `$1000`, whichever comes first. |
|
108
|
|
|
109
|
--- |
|
110
|
|
|
111
|
## Promotion Rules |
|
112
|
|
|
113
|
A promotion can be restricted by a set of rules, each rule is represented by the [PromotionRule data model](/references/promotion/models/PromotionRule). |
|
114
|
|
|
115
|
For example, you can create a promotion that only customers of the `VIP` customer group can use. |
|
116
|
|
|
117
|
 |
|
118
|
|
|
119
|
A `PromotionRule`'s `attribute` property indicates the property's name to which this rule is applied. For example, `customer.groups.id`. |
|
120
|
|
|
121
|
The `attribute` must be an exact key that Medusa recognizes. Refer to [Rule Attributes](#rule-attributes) for the list of supported keys. |
|
122
|
|
|
123
|
The expected value for the attribute is stored in the `PromotionRuleValue` data model. So, a rule can have multiple values. |
|
124
|
|
|
125
|
When testing whether a promotion can be applied to a cart, the rule's `attribute` property and its values are tested on the cart itself. |
|
126
|
|
|
127
|
For example, the cart's customer must be part of the customer group(s) indicated in the promotion rule's value. |
|
128
|
|
|
129
|
### Flexible Rules |
|
130
|
|
|
131
|
The `PromotionRule`'s `operator` property adds more flexibility to the rule’s condition rather than simple equality (`eq`). |
|
132
|
|
|
133
|
For example, to restrict the promotion to only `VIP` and `B2B` customer groups: |
|
134
|
|
|
135
|
- Add a `PromotionRule` record with its `attribute` property set to `customer.groups.id` and `operator` property to `in`. |
|
136
|
- Add two `PromotionRuleValue` records associated with the rule: one with the value `VIP` and the other `B2B`. |
|
137
|
|
|
138
|
 |
|
139
|
|
|
140
|
In this case, a customer’s group must be in the `VIP` and `B2B` set of values to use the promotion. |
|
141
|
|
|
142
|
### Rule Attributes |
|
143
|
|
|
144
|
A rule's `attribute` must be an exact key that Medusa recognizes for the resource the rule targets. The supported keys depend on where you set the rule: |
|
145
|
|
|
146
|
- The promotion's `rules` property: Medusa tests the rule against the cart. |
|
147
|
- The application method's `target_rules` and `buy_rules` properties: Medusa tests the rule against each cart line item or shipping method, based on the application method's `target_type`. |
|
148
|
|
|
149
|
<Note title="Warning" type="warning"> |
|
150
|
|
|
151
|
Item-scoped attributes must be prefixed with `items.`, and shipping-method-scoped attributes with `shipping_methods.`. For example, to target a product, use `items.product.id`, not `product_id`. |
|
152
|
|
|
153
|
If you set an unrecognized attribute on an automatic promotion, Medusa filters the promotion out and never applies it, so the discount stays `0` without an error. |
|
154
|
|
|
155
|
</Note> |
|
156
|
|
|
157
|
The following table lists the supported attributes for each rule scope: |
|
158
|
|
|
159
|
<Table> |
|
160
|
<Table.Header> |
|
161
|
<Table.Row> |
|
162
|
<Table.HeaderCell> |
|
163
|
|
|
164
|
Rule Scope |
|
165
|
|
|
166
|
</Table.HeaderCell> |
|
167
|
<Table.HeaderCell> |
|
168
|
|
|
169
|
Set On |
|
170
|
|
|
171
|
</Table.HeaderCell> |
|
172
|
<Table.HeaderCell> |
|
173
|
|
|
174
|
Attributes |
|
175
|
|
|
176
|
</Table.HeaderCell> |
|
177
|
</Table.Row> |
|
178
|
</Table.Header> |
|
179
|
<Table.Body> |
|
180
|
<Table.Row> |
|
181
|
<Table.Cell> |
|
182
|
|
|
183
|
Cart |
|
184
|
|
|
185
|
</Table.Cell> |
|
186
|
<Table.Cell> |
|
187
|
|
|
188
|
The promotion's `rules` property. |
|
189
|
|
|
190
|
</Table.Cell> |
|
191
|
<Table.Cell> |
|
192
|
|
|
193
|
`customer.groups.id`, `region.id`, `shipping_address.country_code`, `sales_channel_id`, `currency_code` |
|
194
|
|
|
195
|
</Table.Cell> |
|
196
|
</Table.Row> |
|
197
|
<Table.Row> |
|
198
|
<Table.Cell> |
|
199
|
|
|
200
|
Cart items |
|
201
|
|
|
202
|
</Table.Cell> |
|
203
|
<Table.Cell> |
|
204
|
|
|
205
|
The application method's `target_rules` and `buy_rules` properties, when `target_type` is `items`. |
|
206
|
|
|
207
|
</Table.Cell> |
|
208
|
<Table.Cell> |
|
209
|
|
|
210
|
`items.product.id`, `items.product.categories.id`, `items.product.collection_id`, `items.product.type_id`, `items.product.tags.id` |
|
211
|
|
|
212
|
</Table.Cell> |
|
213
|
</Table.Row> |
|
214
|
<Table.Row> |
|
215
|
<Table.Cell> |
|
216
|
|
|
217
|
Shipping methods |
|
218
|
|
|
219
|
</Table.Cell> |
|
220
|
<Table.Cell> |
|
221
|
|
|
222
|
The application method's `target_rules` and `buy_rules` properties, when `target_type` is `shipping_methods`. |
|
223
|
|
|
224
|
</Table.Cell> |
|
225
|
<Table.Cell> |
|
226
|
|
|
227
|
`shipping_methods.shipping_option.shipping_option_type_id` |
|
228
|
|
|
229
|
</Table.Cell> |
|
230
|
</Table.Row> |
|
231
|
</Table.Body> |
|
232
|
</Table> |
|
233
|
|
|
234
|
To retrieve the supported attributes programmatically, use the [listRuleAttributes](/references/js_sdk/admin/Promotion/methods/js_sdk.admin.Promotion.listRuleAttributes) JS SDK method or the [List Rule Attribute Options](!api!/admin/promotions/list-potential-rule-attributes) Admin API route. |
|
235
|
|
|
236
|
### Restrict a Promotion by Order Value |
|
237
|
|
|
238
|
Along with the attributes in the table above, the cart context includes the cart's computed totals, such as `item_subtotal`, `subtotal`, `item_total`, and `total`. You can create a rule on one of these attributes using a numeric operator (`gte`, `lte`, `gt`, or `lt`) to restrict a promotion by order value, such as a minimum spend. |
|
239
|
|
|
240
|
<Note> |
|
241
|
|
|
242
|
These numeric conditions aren't available in the Medusa Admin dashboard. Create them using workflows or the Admin API. |
|
243
|
|
|
244
|
</Note> |
|
245
|
|
|
246
|
For example, to only apply a promotion when the cart's items subtotal is at least `$100`: |
|
247
|
|
|
248
|
```ts |
|
249
|
const { result } = await createPromotionsWorkflow(container) |
|
250
|
.run({ |
|
251
|
input: { |
|
252
|
promotionsData: [{ |
|
253
|
code: "SPEND100", |
|
254
|
type: "standard", |
|
255
|
status: "active", |
|
256
|
application_method: { |
|
257
|
type: "percentage", |
|
258
|
target_type: "items", |
|
259
|
allocation: "across", |
|
260
|
value: 10, |
|
261
|
currency_code: "usd", |
|
262
|
}, |
|
263
|
rules: [ |
|
264
|
{ |
|
265
|
attribute: "item_subtotal", |
|
266
|
operator: "gte", |
|
267
|
values: ["100"], |
|
268
|
}, |
|
269
|
], |
|
270
|
}], |
|
271
|
}, |
|
272
|
}) |
|
273
|
``` |
|
274
|
|
|
275
|
Medusa evaluates the rule against the cart's total when the promotion is applied. Within a single computation, the total doesn't reflect discounts from other promotions applied in the same computation. |
|
276
|
|
|
277
|
--- |
|
278
|
|
|
279
|
## How to Apply Rules on a Promotion? |
|
280
|
|
|
281
|
### Using Workflows |
|
282
|
|
|
283
|
If you're managing promotions using [Medusa's workflows](../../../medusa-workflows-reference/page.mdx) or the API routes that use them, you can specify rules for the promotion or its [application method](../application-method/page.mdx). |
|
284
|
|
|
285
|
For example, if you're creating a promotion using the [createPromotionsWorkflow](/references/medusa-workflows/createPromotionsWorkflow): |
|
286
|
|
|
287
|
```ts |
|
288
|
const { result } = await createPromotionsWorkflow(container) |
|
289
|
.run({ |
|
290
|
input: { |
|
291
|
promotionsData: [{ |
|
292
|
code: "10OFF", |
|
293
|
type: "standard", |
|
294
|
status: "active", |
|
295
|
application_method: { |
|
296
|
type: "percentage", |
|
297
|
target_type: "items", |
|
298
|
allocation: "across", |
|
299
|
value: 10, |
|
300
|
currency_code: "usd", |
|
301
|
}, |
|
302
|
rules: [ |
|
303
|
{ |
|
304
|
attribute: "customer.groups.id", |
|
305
|
operator: "eq", |
|
306
|
values: [ |
|
307
|
"cusgrp_123", |
|
308
|
], |
|
309
|
}, |
|
310
|
], |
|
311
|
}], |
|
312
|
}, |
|
313
|
}) |
|
314
|
``` |
|
315
|
|
|
316
|
In this example, the promotion is restricted to customers with the `cusgrp_123` customer group. |
|
317
|
|
|
318
|
### Using Promotion Module's Service |
|
319
|
|
|
320
|
<Note> |
|
321
|
|
|
322
|
For most use cases, it's recommended to use [workflows](#using-workflows) instead of directly using the module's service. |
|
323
|
|
|
324
|
</Note> |
|
325
|
|
|
326
|
If you're managing promotions using the Promotion Module's service, you can specify rules for the promotion or its [application method](../application-method/page.mdx) in its methods. |
|
327
|
|
|
328
|
For example, if you're creating a promotion with the [createPromotions](/references/promotion/createPromotions) method: |
|
329
|
|
|
330
|
```ts |
|
331
|
const promotions = await promotionModuleService.createPromotions([ |
|
332
|
{ |
|
333
|
code: "50OFF", |
|
334
|
type: "standard", |
|
335
|
status: "active", |
|
336
|
application_method: { |
|
337
|
type: "percentage", |
|
338
|
target_type: "items", |
|
339
|
value: 50, |
|
340
|
}, |
|
341
|
rules: [ |
|
342
|
{ |
|
343
|
attribute: "customer.group.id", |
|
344
|
operator: "eq", |
|
345
|
values: [ |
|
346
|
"cusgrp_123", |
|
347
|
], |
|
348
|
}, |
|
349
|
], |
|
350
|
}, |
|
351
|
]) |
|
352
|
``` |
|
353
|
|
|
354
|
In this example, the promotion is restricted to customers with the `cusgrp_123` customer group. |
|
355
|
|
|
356
|
### How is the Promotion Rule Applied? |
|
357
|
|
|
358
|
A promotion is applied on a resource if its attributes match the promotion's rules. |
|
359
|
|
|
360
|
For example, consider you have the following promotion with a rule that restricts the promotion to a specific customer: |
|
361
|
|
|
362
|
```json |
|
363
|
{ |
|
364
|
"code": "10OFF", |
|
365
|
"type": "standard", |
|
366
|
"status": "active", |
|
367
|
"application_method": { |
|
368
|
"type": "percentage", |
|
369
|
"target_type": "items", |
|
370
|
"allocation": "across", |
|
371
|
"value": 10, |
|
372
|
"currency_code": "usd" |
|
373
|
}, |
|
374
|
"rules": [ |
|
375
|
{ |
|
376
|
"attribute": "customer_id", |
|
377
|
"operator": "eq", |
|
378
|
"values": [ |
|
379
|
"cus_123" |
|
380
|
] |
|
381
|
} |
|
382
|
] |
|
383
|
} |
|
384
|
``` |
|
385
|
|
|
386
|
When you try to apply this promotion on a cart, the cart's `customer_id` is compared to the promotion rule's value based on the specified operator. So, the promotion will only be applied if the cart's `customer_id` is equal to `cus_123`. |