|
1
|
--- |
|
2
|
tags: |
|
3
|
- cart |
|
4
|
- promotion |
|
5
|
- storefront |
|
6
|
products: |
|
7
|
- cart |
|
8
|
- promotion |
|
9
|
--- |
|
10
|
|
|
11
|
import { CodeTabs, CodeTab } from "docs-ui" |
|
12
|
|
|
13
|
export const metadata = { |
|
14
|
title: `Manage Cart Promotions in Storefront`, |
|
15
|
} |
|
16
|
|
|
17
|
# {metadata.title} |
|
18
|
|
|
19
|
In this guide, you'll learn how to manage cart promotions or discounts, including adding, listing, and removing them. |
|
20
|
|
|
21
|
## Add Promotion to Cart |
|
22
|
|
|
23
|
To add a promotion to a cart, use the [Add Promotion API route](!api!/store/carts/add-promotions). |
|
24
|
|
|
25
|
For example: |
|
26
|
|
|
27
|
<Note title="Tip"> |
|
28
|
|
|
29
|
Learn how to install and configure the JS SDK in the [JS SDK documentation](../../../js-sdk/page.mdx). |
|
30
|
|
|
31
|
</Note> |
|
32
|
|
|
33
|
<CodeTabs group="store-request"> |
|
34
|
<CodeTab label="JS SDK" value="js-sdk"> |
|
35
|
|
|
36
|
```ts |
|
37
|
sdk.store.cart.addPromotions(cart.id, { |
|
38
|
promo_codes: [promoCode], |
|
39
|
}) |
|
40
|
.then(({ cart }) => { |
|
41
|
// use cart... |
|
42
|
console.log(cart) |
|
43
|
}) |
|
44
|
``` |
|
45
|
|
|
46
|
</CodeTab> |
|
47
|
<CodeTab label="Fetch API" value="fetch"> |
|
48
|
|
|
49
|
```ts |
|
50
|
fetch(`http://localhost:9000/store/carts/${cart.id}/promotions`, { |
|
51
|
credentials: "include", |
|
52
|
method: "POST", |
|
53
|
headers: { |
|
54
|
"Content-Type": "application/json", |
|
55
|
"x-publishable-api-key": |
|
56
|
process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || "temp", |
|
57
|
}, |
|
58
|
body: JSON.stringify({ |
|
59
|
promo_codes: [promoCode], |
|
60
|
}), |
|
61
|
}) |
|
62
|
.then((res) => res.json()) |
|
63
|
.then(({ cart }) => { |
|
64
|
// use cart... |
|
65
|
console.log(cart) |
|
66
|
}) |
|
67
|
``` |
|
68
|
|
|
69
|
</CodeTab> |
|
70
|
</CodeTabs> |
|
71
|
|
|
72
|
The [Add Promotion API route](!api!/store/carts/add-promotions) accepts the cart ID as a path parameter, and a `promo_codes` request body parameter, which is an array of promotion codes to apply to the cart. |
|
73
|
|
|
74
|
The response includes the updated cart with the applied promotions. You can use this cart object to show the updated promotions to the customer. |
|
75
|
|
|
76
|
--- |
|
77
|
|
|
78
|
## List Cart Promotions |
|
79
|
|
|
80
|
The cart object you retrieve in the storefront has a `promotions` field, which is an array of the promotions applied to the cart. |
|
81
|
|
|
82
|
You can refer to the [API reference](!api!/store/carts/schema) for the expected fields in a promotion object. |
|
83
|
|
|
84
|
A promotion mainly has a `code` field. You can use that code to display the applied promotions to the customer, and later to [remove a promotion](#remove-promotion-from-cart) from the cart. |
|
85
|
|
|
86
|
For example, if you have a cart object retrieved from Medusa's Store APIs, you can list its promotions as follows: |
|
87
|
|
|
88
|
```tsx |
|
89
|
<div> |
|
90
|
{cart?.promotions?.length ? ( |
|
91
|
<ul> |
|
92
|
{cart.promotions.map((promo) => ( |
|
93
|
<li key={promo.id}>{promo.code} |
|
94
|
<button onClick={() => removePromotion(promo.code!)}>Remove</button> |
|
95
|
</li> |
|
96
|
))} |
|
97
|
</ul> |
|
98
|
) : ( |
|
99
|
<p>No promotions applied</p> |
|
100
|
)} |
|
101
|
</div> |
|
102
|
``` |
|
103
|
|
|
104
|
In the above example, you show the list of applied promotions, and for each promotion, you provide a button to [remove it from the cart](#remove-promotion-from-cart). |
|
105
|
|
|
106
|
<Note title="Tip"> |
|
107
|
|
|
108
|
To show the total discount amount from the applied promotions, refer to the [Cart Totals guide](../totals/page.mdx). |
|
109
|
|
|
110
|
</Note> |
|
111
|
|
|
112
|
--- |
|
113
|
|
|
114
|
## Remove Promotion from Cart |
|
115
|
|
|
116
|
To remove a promotion from a cart, use the [Remove Promotion API route](!api!/store/carts/remove-promotions-from-cart). |
|
117
|
|
|
118
|
For example: |
|
119
|
|
|
120
|
<CodeTabs group="store-request"> |
|
121
|
<CodeTab label="JS SDK" value="js-sdk"> |
|
122
|
|
|
123
|
```ts |
|
124
|
sdk.store.cart.removePromotions(cart.id, { |
|
125
|
promo_codes: [code], |
|
126
|
}) |
|
127
|
.then(({ cart }) => { |
|
128
|
// use cart... |
|
129
|
console.log(cart) |
|
130
|
}) |
|
131
|
``` |
|
132
|
|
|
133
|
</CodeTab> |
|
134
|
<CodeTab label="Fetch API" value="fetch"> |
|
135
|
|
|
136
|
```ts |
|
137
|
fetch(`http://localhost:9000/store/carts/${cart.id}/promotions`, { |
|
138
|
credentials: "include", |
|
139
|
method: "DELETE", |
|
140
|
headers: { |
|
141
|
"Content-Type": "application/json", |
|
142
|
"x-publishable-api-key": |
|
143
|
process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || "temp", |
|
144
|
}, |
|
145
|
body: JSON.stringify({ |
|
146
|
promo_codes: [code], |
|
147
|
}), |
|
148
|
}) |
|
149
|
.then((res) => res.json()) |
|
150
|
.then(({ cart }) => { |
|
151
|
// use cart... |
|
152
|
console.log(cart) |
|
153
|
}) |
|
154
|
``` |
|
155
|
|
|
156
|
</CodeTab> |
|
157
|
</CodeTabs> |
|
158
|
|
|
159
|
The [Remove Promotion API route](!api!/store/carts/remove-promotions-from-cart) accepts the cart ID as a path parameter, and a `promo_codes` request body parameter, which is an array of promotion codes to remove from the cart. |
|
160
|
|
|
161
|
The response includes the updated cart with the removed promotions. You can use this cart object to show the updated promotions to the customer. |