Search.../

updateLocation( )

Developer Preview

Updates a location.

Description

The updateLocation() function returns a Promise that resolves to the updated location.

Each time a location is updated, its revision increments by 1. The current revision must be passed when updating the comment. This ensures you're working with the latest location and prevents unintended overwrites.

Notes:

  • Currently, it isn't possible to partially update a location. You must pass the full location object in the location parameter.
  • Any fields which are not included in the location parameter will be overwritten to null.
Admin Method

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

Syntax

function updateLocation(_id: string, location: UpdateLocation): Promise<Location>

updateLocation Parameters

NAME
TYPE
DESCRIPTION
_id
string

Location ID.

location
UpdateLocation

Updated location details.

Returns

Updated location.

Return Type:

Promise<
Location
>
NAME
TYPE
DESCRIPTION
_id
string

Location ID.

address
Address

Address.

archived
boolean

Whether the location is archived. Archived locations can't be updated.

Note: Archiving a location doesn't affect its status.

businessSchedule
BusinessSchedule

Business schedule. Array of weekly recurring time periods when the location is open for business. Limited to 100 time periods.

Note: Not supported by Wix Bookings.

default
boolean

Whether this is the default location. There can only be one default location per site. The default location can't be archived.

description
string

Location description.

email
string

Email address.

fax
string

Fax number.

locationType
string

Location type.

Supported values: BRANCH, OFFICES, RECEPTION, HEADQUARTERS.

Note: This field is currently not supported.

name
string

Location name.

phone
string

Phone number.

revision
string

Revision number, which increments by 1 each time the location is updated. To prevent conflicting changes, the existing revision must be used when updating a location.

status
string

Location status.

Supported values: ACTIVE, INACTIVE.

Default: ACTIVE

Notes:

doesn't affect the location's status.

  • INACTIVE status is currently not supported.
timeZone
string

Timezone in America/New_York format.

Was this helpful?

Update a location's details (dashboard page code)

Copy Code
1import { locations } from 'wix-business-tools.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample _id value: '4ae3bbc8-fb78-4eb7-8bc5-1fec19c7dbfc'
5 *
6 * Sample location value:
7 * {
8 * address: {
9 * city: 'Roma',
10 * country: 'IT',
11 * formatted: 'Via Roma 123, 00100 Roma',
12 * location: {
13 * latitude: 41.9028,
14 * longitude: 12.4964
15 * },
16 * postalCode: '00100',
17 * streetAddress: {
18 * apt: '',
19 * name: 'Via Roma',
20 * number: '123'
21 * },
22 * subdivision: 'IT-RM'
23 * },
24 * businessSchedule: {
25 * periods: [
26 * {
27 * closeDay: 'SATURDAY',
28 * closeTime: '24:00',
29 * openDay: 'MONDAY',
30 * openTime: '00:00'
31 * }
32 * ],
33 * specialHourPeriod: [
34 * {
35 * comment: 'Half price in the lead up to Christmas!',
36 * endDate: '2023-12-24T23:59:00Z',
37 * isClosed: false,
38 * startDate: '2023-12-01T00:00:00Z'
39 * }
40 * ]
41 * },
42 * description: 'Our store in Rome is open 24/6!',
43 * email: '24.6.rome@example.com',
44 * fax: '+39 06 1234 5679',
45 * name: 'Rome 24/6',
46 * phone: '+39 06 1234 5678',
47 * status: 'ACTIVE',
48 * timeZone: 'Europe/Rome'
49 * }
50 */
51
52export async function myCreateLocationFunction(_id, location) {
53 try {
54 const elevatedCreateLocation = elevate(locations.createLocation);
55 const updatedLocation = await elevatedCreateLocation(_id, location);
56 console.log('Location details have been successfully updated:', updatedLocation);
57
58 return updatedLocation;
59 } catch (error) {
60 console.error(error);
61 // Handle the error
62 }
63}
64
65/* Promise resolves to:
66 * {
67 * "_id": "4ae3bbc8-fb78-4eb7-8bc5-1fec19c7dbfc"
68 * "address": {
69 * "city": "Roma",
70 * "country": "IT",
71 * "formatted": "Via Roma 123, 00100 Roma",
72 * "location": {
73 * "latitude": 41.9028,
74 * "longitude": 12.4964
75 * },
76 * "postalCode": "00100",
77 * "streetAddress": {
78 * "apt": "",
79 * "name": "Via Roma",
80 * "number": "123"
81 * },
82 * "subdivision": 'IT-RM'
83 * },
84 * "archived": false,
85 * "businessSchedule": {
86 * "periods": [
87 * {
88 * "closeDay": "MONDAY",
89 * "closeTime": "00:00",
90 * "openDay": "SATURDAY",
91 * "openTime": "24:00"
92 * }
93 * ],
94 * "specialHourPeriod": [
95 * {
96 * "comment": "Half price in the lead up to Christmas!",
97 * "endDate": "2023-12-24T23:59:00Z",
98 * "isClosed": false,
99 * "startDate": "2023-12-01T00:00:00Z"
100 * }
101 * ]
102 * },
103 * "default": false,
104 * "description": "Our store in Rome is open 24/6!",
105 * "email": "24.6.rome@example.com",
106 * "fax": "+39 06 1234 5679",
107 * "name": "Rome 24/6",
108 * "phone": "+39 06 1234 5678",
109 * "revision": "2",
110 * "status": "ACTIVE",
111 * "timeZone": "Europe/Rome"
112 * }
113 */
114
Update a location's details (export from backend code)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { locations } from 'wix-business-tools.v2';
3import { elevate } from 'wix-auth';
4
5/* Sample _id value: '4ae3bbc8-fb78-4eb7-8bc5-1fec19c7dbfc'
6 *
7 * Sample location value:
8 * {
9 * address: {
10 * city: 'Roma',
11 * country: 'IT',
12 * formatted: 'Via Roma 123, 00100 Roma',
13 * location: {
14 * latitude: 41.9028,
15 * longitude: 12.4964
16 * },
17 * postalCode: '00100',
18 * streetAddress: {
19 * apt: '',
20 * name: 'Via Roma',
21 * number: '123'
22 * },
23 * subdivision: 'IT-RM'
24 * },
25 * businessSchedule: {
26 * periods: [
27 * {
28 * closeDay: 'SATURDAY',
29 * closeTime: '24:00',
30 * openDay: 'MONDAY',
31 * openTime: '00:00'
32 * }
33 * ],
34 * specialHourPeriod: [
35 * {
36 * comment: 'Half price in the lead up to Christmas!',
37 * endDate: '2023-12-24T23:59:00Z',
38 * isClosed: false,
39 * startDate: '2023-12-01T00:00:00Z'
40 * }
41 * ]
42 * },
43 * description: 'Our store in Rome is open 24/6!',
44 * email: '24.6.rome@example.com',
45 * fax: '+39 06 1234 5679',
46 * name: 'Rome 24/6',
47 * phone: '+39 06 1234 5678',
48 * status: 'ACTIVE',
49 * timeZone: 'Europe/Rome'
50 * }
51 */
52
53export const myCreateLocationFunction = webMethod(Permissions.Anyone, async (_id, location) => {
54 try {
55 const elevatedCreateLocation = elevate(locations.createLocation);
56 const updatedLocation = await elevatedCreateLocation(_id, location);
57 console.log('Location details have been successfully updated:', updatedLocation);
58
59 return updatedLocation;
60 } catch (error) {
61 console.error(error);
62 // Handle the error
63 }
64});
65
66/* Promise resolves to:
67 * {
68 * "_id": "4ae3bbc8-fb78-4eb7-8bc5-1fec19c7dbfc"
69 * "address": {
70 * "city": "Roma",
71 * "country": "IT",
72 * "formatted": "Via Roma 123, 00100 Roma",
73 * "location": {
74 * "latitude": 41.9028,
75 * "longitude": 12.4964
76 * },
77 * "postalCode": "00100",
78 * "streetAddress": {
79 * "apt": "",
80 * "name": "Via Roma",
81 * "number": "123"
82 * },
83 * "subdivision": 'IT-RM'
84 * },
85 * "archived": false,
86 * "businessSchedule": {
87 * "periods": [
88 * {
89 * "closeDay": "MONDAY",
90 * "closeTime": "00:00",
91 * "openDay": "SATURDAY",
92 * "openTime": "24:00"
93 * }
94 * ],
95 * "specialHourPeriod": [
96 * {
97 * "comment": "Half price in the lead up to Christmas!",
98 * "endDate": "2023-12-24T23:59:00Z",
99 * "isClosed": false,
100 * "startDate": "2023-12-01T00:00:00Z"
101 * }
102 * ]
103 * },
104 * "default": false,
105 * "description": "Our store in Rome is open 24/6!",
106 * "email": "24.6.rome@example.com",
107 * "fax": "+39 06 1234 5679",
108 * "name": "Rome 24/6",
109 * "phone": "+39 06 1234 5678",
110 * "revision": "2",
111 * "status": "ACTIVE",
112 * "timeZone": "Europe/Rome"
113 * }
114 */
115
Overwrite a location's data

This code uses the user's input into a form on the page to overwrite the data of a location which they choose from a dropdown list on the page. To make this accessible to only company managers so they can update locations in the database, make a members-only page.

Copy Code
1/*****************************************
2 * Backend code - update-location.web.js *
3 ****************************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import { locations } from 'wix-business-tools.v2';
7import { elevate } from 'wix-auth';
8
9export const updateLocation = webMethod(Permissions.Anyone, async (locationId, location) => {
10 try {
11 const elevatedUpdateLocation = elevate(locations.updateLocation);
12 const updatedLocation = await elevatedUpdateLocation(locationId, location);
13
14 return updatedLocation;
15 } catch (error) {
16 console.error(error);
17 throw new Error(error);
18 }
19});
20
21export const listLocations = webMethod(Permissions.Anyone, async () => {
22 try {
23 const elevatedListLocations = elevate(locations.listLocations);
24 const results = await elevatedListLocations();
25
26 return results.locations;
27 } catch (error) {
28 console.error(error);
29 throw new Error(error);
30 }
31});
32
33export const getLocationById = webMethod(Permissions.Anyone, async (locationId) => {
34 try {
35 const elevatedGetLocation = elevate(locations.getLocation);
36 const myLocation = await elevatedGetLocation(locationId);
37
38 return myLocation;
39 } catch (error) {
40 console.error(error);
41 throw new Error(error);
42 }
43});
44
45
46/*************
47 * Page code *
48 ************/
49
50import { updateLocation, listLocations, getLocationById } from 'backend/update-location.web';
51
52$w.onReady(async () => {
53 await populateStoresDropdown();
54
55 $w('#update').onClick(async () => {
56 const locationId = $w('#locationsDropdown').value;
57 const currentLocationValues = await getLocationById(locationId);
58
59 const location = {
60 name: $w('#newName').value,
61 address: {
62 streetAddress: {
63 number: $w('#streetNumber').value,
64 name: $w('#streetName').value
65 },
66 city: $w('#city').value,
67 postalCode: $w('#postalCode').value,
68 },
69 timeZone: $w('#timeZone').value,
70 description: $w('#description').value,
71 email: $w('#email').value,
72 phone: $w('#phone').value,
73 // Must use the current revision value
74 revision: currentLocationValues.revision
75 };
76
77 const updatedLocation = await updateLocation(locationId, location);
78 console.log('Location details have been successfully updated to the following:\n', updatedLocation);
79 $w('#updateSuccessMsg').show();
80 });
81});
82
83async function populateStoresDropdown() {
84 const locations = await listLocations();
85 const dropdownOptions = locations.map((location) => {
86 return {
87 label: location.name,
88 value: location._id
89 };
90 });
91
92 $w('#locationsDropdown').options = dropdownOptions;
93};
Partially update a location's data without overwriting other properties

This code shows an example of the user using an input form to change just a couple ofa location's properties - phone and email - while avoiding overwriting the other properties with a null value. This page is accessible to only company managers so they can update locations in the database.

Copy Code
1/*****************************************
2 * Backend code - update-location.web.js *
3 ****************************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import { locations } from 'wix-business-tools.v2';
7import { elevate } from 'wix-auth';
8
9export const updateLocation = webMethod(Permissions.Anyone, async (locationId, location) => {
10 try {
11 const elevatedUpdateLocation = elevate(locations.updateLocation);
12 const updatedLocation = await elevatedUpdateLocation(locationId, location);
13
14 return updatedLocation;
15 } catch (error) {
16 console.error(error);
17 throw new Error(error);
18 }
19});
20
21export const listLocations = webMethod(Permissions.Anyone, async () => {
22 try {
23 const elevatedListLocations = elevate(locations.listLocations);
24 const results = await elevatedListLocations();
25
26 return results.locations;
27 } catch (error) {
28 console.error(error);
29 throw new Error(error);
30 }
31});
32
33export const getLocationById = webMethod(Permissions.Anyone, async (locationId) => {
34 try {
35 const elevatedGetLocation = elevate(locations.getLocation);
36 const myLocation = await elevatedGetLocation(locationId);
37
38 return myLocation;
39 } catch (error) {
40 console.error(error);
41 throw new Error(error);
42 }
43});
44
45
46/*************
47 * Page code *
48 ************/
49
50import { updateLocation, listLocations, getLocationById } from 'backend/update-location.web';
51
52$w.onReady(async () => {
53 await populateStoresDropdown();
54
55 $w('#update').onClick(async () => {
56 // set location to current value and only change relevant properties' values
57 const locationId = $w('#locationsDropdown').value;
58 const location = await getLocationById(locationId);
59
60 location.email = $w('#email').value;
61 location.phone = $w('#phone').value;
62
63 const updatedLocation = await updateLocation(locationId, location);
64 console.log('Location details have been successfully updated to the following:\n', updatedLocation);
65 $w('#updateSuccessMsg').show();
66 });
67});
68
69async function populateStoresDropdown() {
70 const locations = await listLocations();
71 const dropdownOptions = locations.map((location) => {
72 return {
73 label: location.name,
74 value: location._id
75 };
76 });
77
78 $w('#locationsDropdown').options = dropdownOptions;
79}