|
1
|
--- |
|
2
|
tags: |
|
3
|
- cart |
|
4
|
- storefront |
|
5
|
products: |
|
6
|
- cart |
|
7
|
- customer |
|
8
|
--- |
|
9
|
|
|
10
|
import { CodeTabs, CodeTab, Prerequisites } from "docs-ui" |
|
11
|
|
|
12
|
export const metadata = { |
|
13
|
title: `Update Cart in Storefront`, |
|
14
|
} |
|
15
|
|
|
16
|
# {metadata.title} |
|
17
|
|
|
18
|
In this guide, you'll learn how to update different details of a cart. |
|
19
|
|
|
20
|
<Note title="Tip"> |
|
21
|
|
|
22
|
This guide doesn't cover updating the cart's line items. For that, refer to the [Manage Cart's Items in Storefront](../manage-items/page.mdx) guide. |
|
23
|
|
|
24
|
</Note> |
|
25
|
|
|
26
|
## Update Cart's Region |
|
27
|
|
|
28
|
If a customer changes their region, you must update their cart to be associated with that region. |
|
29
|
|
|
30
|
For example: |
|
31
|
|
|
32
|
<Note title="Tip"> |
|
33
|
|
|
34
|
Learn how to install and configure the JS SDK in the [JS SDK documentation](../../../js-sdk/page.mdx). |
|
35
|
|
|
36
|
</Note> |
|
37
|
|
|
38
|
export const updateRegionHighlights = [ |
|
39
|
["2", `"new_id"`, "Pass the new chosen region's ID."] |
|
40
|
] |
|
41
|
|
|
42
|
```ts highlights={updateRegionHighlights} |
|
43
|
sdk.store.cart.update(cartId, { |
|
44
|
region_id: "new_id", |
|
45
|
}) |
|
46
|
.then(({ cart }) => { |
|
47
|
// use cart... |
|
48
|
console.log(cart) |
|
49
|
}) |
|
50
|
``` |
|
51
|
|
|
52
|
The [Update Cart API route](!api!/store/carts/update-a-cart) accepts a `region_id` request body parameter, whose value is the new region to associate with the cart. |
|
53
|
|
|
54
|
--- |
|
55
|
|
|
56
|
## Set Cart's Customer |
|
57
|
|
|
58
|
You might need to change the cart's customer in two cases: |
|
59
|
|
|
60
|
- You created the cart for the customer as a guest, then they logged-in, so you want to associate the cart with them as a registered customer. |
|
61
|
- You're transferring the cart from one customer to another, which is useful in company setups, such as when implementing B2B commerce applications. |
|
62
|
|
|
63
|
To set or change the cart's customer, send an authenticated `POST` request to the Set Cart's Customer API route: |
|
64
|
|
|
65
|
<Note> |
|
66
|
|
|
67
|
This API route is only available after [Medusa v2.0.5](https://github.com/medusajs/medusa/releases/tag/v2.0.5). |
|
68
|
|
|
69
|
</Note> |
|
70
|
|
|
71
|
```ts |
|
72
|
// TODO must be authenticated as the customer to set the cart's customer |
|
73
|
sdk.store.cart.transferCart(cartId) |
|
74
|
.then(({ cart }) => { |
|
75
|
// use cart... |
|
76
|
console.log(cart) |
|
77
|
}) |
|
78
|
``` |
|
79
|
|
|
80
|
Assuming the JS SDK is configured to send an authenticated request, the cart is now associated with the logged-in customer. |
|
81
|
|
|
82
|
Learn more about authenticating customers with the JS SDK in the [Login Customer guide](../../customers/login/page.mdx). |
|
83
|
|
|
84
|
<Note title="Tip"> |
|
85
|
|
|
86
|
When using the Fetch API to send the request, either use `credentials: include` if the customer is already authenticated with a cookie session, or pass the Authorization Bearer token in the request's header. |
|
87
|
|
|
88
|
</Note> |
|
89
|
|
|
90
|
--- |
|
91
|
|
|
92
|
## Set Cart's Locale |
|
93
|
|
|
94
|
<Prerequisites |
|
95
|
items={[ |
|
96
|
{ |
|
97
|
text: "Translation Module Configured", |
|
98
|
link: "/commerce-modules/translation#configure-translation-module", |
|
99
|
} |
|
100
|
]} |
|
101
|
/> |
|
102
|
|
|
103
|
By default, items in the cart will have their associated product's original content. If your storefront supports [localization](../../localization/page.mdx), you can set the cart's locale to ensure that the item contents are in the customer's preferred language. |
|
104
|
|
|
105
|
You can set the cart's locale by passing the `locale` request body parameter to the [Update Cart API route](!api!/store/carts/create-cart). For example: |
|
106
|
|
|
107
|
```ts |
|
108
|
sdk.store.cart.update(cartId, { |
|
109
|
// you can also retrieve the locale from sdk.getLocale() |
|
110
|
locale: "fr-FR", |
|
111
|
}) |
|
112
|
.then(({ cart }) => { |
|
113
|
// TODO use the cart... |
|
114
|
console.log(cart) |
|
115
|
}) |
|
116
|
``` |
|
117
|
|
|
118
|
When the cart's locale is set, existing and new items in the cart will have their content in the specified locale if translations are available. |