|
1
|
--- |
|
2
|
tags: |
|
3
|
- region |
|
4
|
- storefront |
|
5
|
products: |
|
6
|
- region |
|
7
|
--- |
|
8
|
|
|
9
|
export const metadata = { |
|
10
|
title: `Store Selected Region in Storefront`, |
|
11
|
} |
|
12
|
|
|
13
|
# {metadata.title} |
|
14
|
|
|
15
|
In this guide, you'll learn how to store a customer's selected region in your storefront, then retrieve it for later use. |
|
16
|
|
|
17
|
## Why Select and Store Region? |
|
18
|
|
|
19
|
In your store, prices, taxes, and available payment methods can vary between regions. |
|
20
|
|
|
21
|
So, you should allow customers to select their region to see the correct prices, taxes, and payment methods. |
|
22
|
|
|
23
|
--- |
|
24
|
|
|
25
|
## Store Selected Region ID |
|
26
|
|
|
27
|
To allow the customer to select a region, you should show them the list of regions as explained in the [List Regions](../list/page.mdx) guide. You can show them in a select input, for example. |
|
28
|
|
|
29
|
Then, once the customer selects their region, store that region's ID in the `localStorage`. |
|
30
|
|
|
31
|
For example: |
|
32
|
|
|
33
|
```ts |
|
34
|
localStorage.setItem("region_id", region.id) |
|
35
|
``` |
|
36
|
|
|
37
|
Then, you can retrieve it later using the `localSorage.getItem` method: |
|
38
|
|
|
39
|
```ts |
|
40
|
const regionId = localStorage.getItem("region_id") |
|
41
|
``` |
|
42
|
|
|
43
|
--- |
|
44
|
|
|
45
|
## Retrieve Selected Region's Details |
|
46
|
|
|
47
|
As you build your storefront, you may need to retrieve the full details of a region. For example, to get the region's currency code. |
|
48
|
|
|
49
|
To retrieve the selected region's details from Medusa, use the [Retrieve Region API route](!api!/store/regions/get-a-region): |
|
50
|
|
|
51
|
<Note title="Tip"> |
|
52
|
|
|
53
|
Learn how to install and configure the JS SDK in the [JS SDK documentation](../../../js-sdk/page.mdx). |
|
54
|
|
|
55
|
</Note> |
|
56
|
|
|
57
|
```ts |
|
58
|
const regionId = localStorage.getItem("region_id") |
|
59
|
|
|
60
|
sdk.store.region.retrieve(regionId) |
|
61
|
.then(({ region }) => { |
|
62
|
console.log(region) |
|
63
|
}) |
|
64
|
``` |
|
65
|
|
|
66
|
The response has a `regions` field, which is an array of [regions](!api!/store/regions/schema). |
|
67
|
|
|
68
|
--- |
|
69
|
|
|
70
|
## Store Region Details in React Context |
|
71
|
|
|
72
|
If you're using React, it's then recommended to create a context that stores the region details and make it available to all components in your application, as explained in the [Region React Context in Storefront](../context/page.mdx) guide. |