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

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 */
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.jsw *
3 *************************************/
4
5import { locations } from 'wix-business-tools.v2';
6import { elevate } from 'wix-auth';
7
8export async function updateLocation(locationId, location) {
9 try {
10 const elevatedUpdateLocation = elevate(locations.updateLocation);
11 const updatedLocation = await elevatedUpdateLocation(locationId, location);
12
13 return updatedLocation;
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
32export async function getLocationById(locationId) {
33 try {
34 const elevatedGetLocation = elevate(locations.getLocation);
35 const myLocation = await elevatedGetLocation(locationId);
36
37 return myLocation;
38 } catch (error) {
39 console.error(error);
40 throw new Error(error);
41 }
42}
43
44
45/*************
46 * Page code *
47 ************/
48
49import { updateLocation, listLocations, getLocationById } from 'backend/update-location';
50
51$w.onReady(async () => {
52 await populateStoresDropdown();
53
54 $w('#update').onClick(async () => {
55 const locationId = $w('#locationsDropdown').value;
56 const currentLocationValues = await getLocationById(locationId);
57
58 const location = {
59 name: $w('#newName').value,
60 address: {
61 streetAddress: {
62 number: $w('#streetNumber').value,
63 name: $w('#streetName').value
64 },
65 city: $w('#city').value,
66 postalCode: $w('#postalCode').value,
67 },
68 timeZone: $w('#timeZone').value,
69 description: $w('#description').value,
70 email: $w('#email').value,
71 phone: $w('#phone').value,
72 // Must use the current revision value
73 revision: currentLocationValues.revision
74 };
75
76 const updatedLocation = await updateLocation(locationId, location);
77 console.log('Location details have been successfully updated to the following:\n', updatedLocation);
78 $w('#updateSuccessMsg').show();
79 });
80});
81
82async function populateStoresDropdown() {
83 const locations = await listLocations();
84 const dropdownOptions = locations.map((location) => {
85 return {
86 label: location.name,
87 value: location._id
88 };
89 });
90
91 $w('#locationsDropdown').options = dropdownOptions;
92};
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.jsw *
3 *************************************/
4
5import { locations } from 'wix-business-tools.v2';
6import { elevate } from 'wix-auth';
7
8export async function updateLocation(locationId, location) {
9 try {
10 const elevatedUpdateLocation = elevate(locations.updateLocation);
11 const updatedLocation = await elevatedUpdateLocation(locationId, location);
12
13 return updatedLocation;
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
32export async function getLocationById(locationId) {
33 try {
34 const elevatedGetLocation = elevate(locations.getLocation);
35 const myLocation = await elevatedGetLocation(locationId);
36
37 return myLocation;
38 } catch (error) {
39 console.error(error);
40 throw new Error(error);
41 }
42}
43
44
45/*************
46 * Page code *
47 ************/
48
49import { updateLocation, listLocations, getLocationById } from 'backend/update-location';
50
51$w.onReady(async () => {
52 await populateStoresDropdown();
53
54 $w('#update').onClick(async () => {
55 // set location to current value and only change relevant properties' values
56 const locationId = $w('#locationsDropdown').value;
57 const location = await getLocationById(locationId);
58
59 location.email = $w('#email').value;
60 location.phone = $w('#phone').value;
61
62 const updatedLocation = await updateLocation(locationId, location);
63 console.log('Location details have been successfully updated to the following:\n', updatedLocation);
64 $w('#updateSuccessMsg').show();
65 });
66});
67
68async function populateStoresDropdown() {
69 const locations = await listLocations();
70 const dropdownOptions = locations.map((location) => {
71 return {
72 label: location.name,
73 value: location._id
74 };
75 });
76
77 $w('#locationsDropdown').options = dropdownOptions;
78}