Search.../

listLocations( )

Developer Preview

Retrieves a list of locations.

Description

Retrieves a list of up to 1,000 locations, given the provided filters, sorting, and paging.

Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function listLocations(options: ListLocationsOptions): Promise<ListLocationsResponse>

listLocations Parameters

NAME
TYPE
DESCRIPTION
options
Optional
ListLocationsOptions

Options to use when retrieving a list of locations.

Returns

Return Type:

Promise<
ListLocationsResponse
>
NAME
TYPE
DESCRIPTION
locations
Array<
Location
>

Retrieved locations.

pagingMetadata
PagingMetadata

Paging info.

Was this helpful?

List all locations

Copy Code
1import { locations } from 'wix-business-tools.v2';
2import { elevate } from 'wix-auth';
3
4export async function myListLocationsFunction() {
5 try {
6 const elevatedListLocations = elevate(locations.listLocations);
7 const myLocations = await elevatedListLocations();
8 console.log('Locations:', myLocations);
9
10 return myLocations;
11 } catch (error) {
12 console.error(error);
13 // Handle the error
14 }
15}
16
17/* Promise resolves to:
18 * {
19 * "locations": [
20 * {
21 * "_id": "6a7a7356-a122-4de6-943c-3ea9e66f0d0a",
22 * "address": {
23 * "city": "Costa Mesa",
24 * "country": "US",
25 * "formatted": "Location1980, Placentia Avenue, Costa Mesa, CA, USA",
26 * "location": {
27 * "latitude": 33.6463497,
28 * "longitude": -117.931867
29 * },
30 * "postalCode": "92627"
31 * "streetAddress": {
32 * "apt": "",
33 * "name": "Placentia Avenue",
34 * "number": "1980"
35 * },
36 * "subdivision": "CA",
37 * },
38 * "archived": false,
39 * "businessSchedule": {
40 * "periods": [],
41 * "specialHourPeriod": []
42 * },
43 * "default": true,
44 * "email": "",
45 * "fax": "",
46 * "name": "Costa Mesa Store",
47 * "phone": "",
48 * "revision": "1",
49 * "status": "ACTIVE",
50 * "timeZone": "America/Los_Angeles"
51 * },
52 * {
53 * "_id": "6a0c5611-0610-4fc2-9eda-a5614ffaf141",
54 * "address": {
55 * "city": "Kingston",
56 * "country": "CA",
57 * "formatted": "222, Stuart Street, Kingston, ON, Canada",
58 * "location": {
59 * "latitude": 44.2236494,
60 * "longitude": -76.4992216
61 * },
62 * "postalCode": "K7L 2W1"
63 * "streetAddress": {
64 * "apt": "",
65 * "name": "Stuart Street",
66 * "number": "222"
67 * },
68 * "subdivision": "ON",
69 * },
70 * "archived": false,
71 * "businessSchedule": {
72 * "periods": [],
73 * "specialHourPeriod": []
74 * },
75 * "default": false,
76 * "email": "",
77 * "fax": "",
78 * "name": "Kingston Store",
79 * "phone": "",
80 * "revision": "1",
81 * "status": "ACTIVE",
82 * "timeZone": "America/Los_Angeles"
83 * }
84 * ],
85 * "pagingMetadata": {
86 * "count": 2,
87 * "hasNext": false
88 * }
89 * }
90 */
List all locations

Copy Code
1import { locations } from 'wix-business-tools.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample options value:
5 * {
6 * includeArchived: true,
7 * paging: {
8 * limit: 3,
9 * offset: 0
10 * },
11 * sort: {
12 * fieldName: 'name',
13 * order: 'ASC'
14 * }
15 * }
16 */
17
18export async function myListLocationsFunction(options) {
19 try {
20 const elevatedListLocations = elevate(locations.listLocations);
21 const myLocations = await elevatedListLocations(options);
22 console.log('Locations:', myLocations);
23
24 return myLocations;
25 } catch (error) {
26 console.error(error);
27 // Handle the error
28 }
29}
30
31/* Promise resolves to:
32 * {
33 * "locations": [
34 * {
35 * "_id": "6a0c5611-0610-4fc2-9eda-a5614ffaf141",
36 * "address": {
37 * "city": "Buenos Aires",
38 * "country": "AR",
39 * "formatted": "456, Example Street, Buenos Aires, Argentina",
40 * "location": {
41 * "latitude": -34.6037,
42 * "longitude": -58.3816
43 * },
44 * "postalCode": "C1234",
45 * "streetAddress": {
46 * "apt": "",
47 * "name": "Example Street",
48 * "number": "456"
49 * },
50 * "subdivision": ""
51 * },
52 * "archived": false,
53 * "businessSchedule": {
54 * "periods": [],
55 * "specialHourPeriod": []
56 * },
57 * "default": false,
58 * "email": "",
59 * "fax": "",
60 * "name": "Buenos Aires Store",
61 * "phone": "",
62 * "revision": "1",
63 * "status": "ACTIVE",
64 * "timeZone": "America/Argentina/Buenos_Aires"
65 * },
66 * {
67 * "_id": "6a0c5611-0610-4fc2-9eda-a5614ffaf142",
68 * "address": {
69 * "city": "Paris",
70 * "country": "FR",
71 * "formatted": "789, Rue de l'Exemple, Paris, France",
72 * "location": {
73 * "latitude": 48.8566,
74 * "longitude": 2.3522
75 * },
76 * "postalCode": "75001",
77 * "streetAddress": {
78 * "apt": "",
79 * "name": "Rue de l'Exemple",
80 * "number": "789"
81 * },
82 * "subdivision": ""
83 * },
84 * "archived": false,
85 * "businessSchedule": {
86 * "periods": [],
87 * "specialHourPeriod": []
88 * },
89 * "default": false,
90 * "email": "",
91 * "fax": "",
92 * "name": "Paris Store",
93 * "phone": "",
94 * "revision": "1",
95 * "status": "ACTIVE",
96 * "timeZone": "Europe/Paris"
97 * },
98 * {
99 * "_id": "6a7a7356-a122-4de6-943c-3ea9e66f0d0a",
100 * "address": {
101 * "city": "Vienna",
102 * "country": "AT",
103 * "formatted": "Examplestraße 123, Vienna, Austria",
104 * "location": {
105 * "latitude": 48.8566,
106 * "longitude": 2.3522
107 * },
108 * "postalCode": "12345",
109 * "streetAddress": {
110 * "apt": "",
111 * "name": "Examplestraße",
112 * "number": "123"
113 * },
114 * "subdivision": ""
115 * },
116 * "archived": false,
117 * "businessSchedule": {
118 * "periods": [],
119 * "specialHourPeriod": []
120 * },
121 * "default": true,
122 * "email": "",
123 * "fax": "",
124 * "name": "Vienna Store",
125 * "phone": "",
126 * "revision": "1",
127 * "status": "ACTIVE",
128 * "timeZone": "Europe/Vienna"
129 * }
130 * ],
131 * "pagingMetadata": {
132 * "count": 3,
133 * "hasNext": true
134 * }
135 * }
136 */
Archive a location

This code uses the value of user's chosen location from a dropdown on the page and archives it.

Copy Code
1/***************************************
2 * Backend code - archive-location.jsw *
3 **************************************/
4
5import { locations } from 'wix-business-tools.v2';
6import { elevate } from 'wix-auth';
7
8export async function archiveLocationById (locationId) {
9 try {
10 const elevatedArchiveLocation = elevate(locations.archiveLocation);
11 const archivedLocation = await elevatedArchiveLocation(locationId);
12
13 return archivedLocation;
14 } catch (error) {
15 console.error(error);
16 throw new Error(error);
17 }
18}
19
20export async function listLocations() {
21 try {
22 const elevatedListLocations = elevate(locations.listLocations);
23 const results = await elevatedListLocations();
24
25 return results.locations;
26 } catch (error) {
27 console.error(error);
28 throw new Error(error);
29 }
30}
31
32
33/*************
34 * Page code *
35 ************/
36
37import { archiveLocationById, listLocations } from 'backend/archive-location';
38
39$w.onReady(async () => {
40 await populateStoresDropdown();
41
42 $w('#archiveLocationBtn').onClick(async () => {
43 const locationId = $w('#locationsDropdown').value;
44 const archivedLocation = await archiveLocationById(locationId);
45
46 console.log('The following location has been archived', archivedLocation);
47 $w('#archivedMessage').show();
48 });
49});
50
51async function populateStoresDropdown() {
52 const locations = await listLocations();
53 const dropdownOptions = locations.map((location) => {
54 return {
55 label: location.name,
56 value: location._id
57 }
58 });
59
60 $w('#locationsDropdown').options = dropdownOptions;
61}