Search.../

createLocation( )

Developer Preview

Creates a location.

Description

The createLocation() function returns a Promise that resolves to the created location.

Admin Method

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

Syntax

function createLocation(location: Location): Promise<Location>

createLocation Parameters

NAME
TYPE
DESCRIPTION
location
Location

Location to create.

Returns

Created 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?

Create a new location (dashboard page code)

Copy Code
1import { locations } from 'wix-business-tools.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample location value:
5 * {
6 * address: {
7 * formatted: '12 5th Street, New York'
8 * },
9 * name: 'New York Store',
10 * timeZone: 'America/New_York'
11 * }
12 */
13
14export async function myCreateLocationFunction(location) {
15 try {
16 const elevatedCreateLocation = elevate(locations.createLocation);
17 const newLocation = await elevatedCreateLocation(location);
18 console.log('Created new location:', newLocation);
19
20 return newLocation;
21 } catch (error) {
22 console.error(error);
23 // Handle the error
24 }
25}
26
27/* Promise resolves to:
28 * {
29 * "_id": "125e9107-2d4b-4790-bb49-37e7983ff9b1",
30 * "address": {
31 * "formatted": "12 5th Street, New York"
32 * },
33 * "archived": false,
34 * "default": false,
35 * "name": "New York Store",
36 * "revision": "1",
37 * "status": "ACTIVE",
38 * "timeZone": "America/New_York"
39 * }
40 */
41
Create a new location (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 location value:
6 * {
7 * address: {
8 * formatted: '12 5th Street, New York'
9 * },
10 * name: 'New York Store',
11 * timeZone: 'America/New_York'
12 * }
13 */
14
15export const myCreateLocationFunction = webMethod(Permissions.Anyone, async (location) => {
16 try {
17 const elevatedCreateLocation = elevate(locations.createLocation);
18 const newLocation = await elevatedCreateLocation(location);
19 console.log('Created new location:', newLocation);
20
21 return newLocation;
22 } catch (error) {
23 console.error(error);
24 // Handle the error
25 }
26});
27
28/* Promise resolves to:
29 * {
30 * "_id": "125e9107-2d4b-4790-bb49-37e7983ff9b1",
31 * "address": {
32 * "formatted": "12 5th Street, New York"
33 * },
34 * "archived": false,
35 * "default": false,
36 * "name": "New York Store",
37 * "revision": "1",
38 * "status": "ACTIVE",
39 * "timeZone": "America/New_York"
40 * }
41 */
42
Create a new location

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { locations } from 'wix-business-tools.v2';
3import { elevate } from 'wix-auth';
4
5/* Sample location value:
6 * {
7 * address: {
8 * city: 'London',
9 * country: 'GB',
10 * formatted: '123 London Street, London',
11 * location: {
12 * latitude: 51.505577,
13 * longitude: -0.068600
14 * },
15 * postalCode: 'N1 1AA',
16 * streetAddress: {
17 * apt: '',
18 * name: 'London Street',
19 * number: '123'
20 * },
21 * subdivision: 'GB-ENG'
22 * },
23 * businessSchedule: {
24 * periods: [
25 * {
26 * closeDay: 'MONDAY',
27 * closeTime: '18:00',
28 * openDay: 'MONDAY',
29 * openTime: '08:00'
30 * },
31 * {
32 * closeDay: 'TUESDAY',
33 * closeTime: '18:00',
34 * openDay: 'TUESDAY',
35 * openTime: '08:00'
36 * },
37 * {
38 * closeDay: 'WEDNESDAY',
39 * closeTime: '18:00',
40 * openDay: 'WEDNESDAY',
41 * openTime: '08:00'
42 * },
43 * {
44 * closeDay: 'THURSDAY',
45 * closeTime: '18:00',
46 * openDay: 'THURSDAY',
47 * openTime: '08:00'
48 * },
49 * {
50 * closeDay: 'FRIDAY',
51 * closeTime: '18:00',
52 * openDay: 'FRIDAY',
53 * openTime: '08:00'
54 * },
55 * ],
56 * specialHourPeriod: [
57 * {
58 * comment: 'Half price in the lead up to Christmas!',
59 * endDate: '2023-12-24T23:59:00Z',
60 * isClosed: false,
61 * startDate: '2023-12-01T00:00:00Z'
62 * }
63 * ]
64 * },
65 * description: 'Our brand new location in London City Center!',
66 * email: 'london@example.com',
67 * fax: '011-44-20-12345678',
68 * name: 'London Store',
69 * phone: '0208 123 4567',
70 * status: 'ACTIVE',
71 * timeZone: 'Europe/London'
72 * }
73 */
74
75export const myCreateLocationFunction = webMethod(Permissions.Anyone, async (location) => {
76 try {
77 const elevatedCreateLocation = elevate(locations.createLocation);
78 const newLocation = await elevatedCreateLocation(location);
79 console.log('Created new location:', newLocation);
80
81 return newLocation;
82 } catch (error) {
83 console.error(error);
84 // Handle the error
85 }
86});
87
88/* Promise resolves to:
89 * {
90 * "_id": "4ae3bbc8-fb78-4eb7-8bc5-1fec19c7dbfc",
91 * "address": {
92 * "city": "London",
93 * "country": "GB",
94 * "formatted": "123 London Street, London",
95 * "location": {
96 * "latitude": 51.505577,
97 * "longitude": -0.0686
98 * },
99 * "postalCode": "N1 1AA",
100 * "streetAddress": {
101 * "apt": "",
102 * "name": "London Street",
103 * "number": "123"
104 * },
105 * "subdivision": "GB-ENG"
106 * },
107 * "archived": false,
108 * "businessSchedule": {
109 * "periods": [
110 * {
111 * "closeDay": "MONDAY",
112 * "closeTime": "18:00",
113 * "openDay": "MONDAY",
114 * "openTime": "08:00"
115 * },
116 * {
117 * "closeDay": "TUESDAY",
118 * "closeTime": "18:00",
119 * "openDay": "TUESDAY",
120 * "openTime": "08:00"
121 * },
122 * {
123 * "closeDay": "WEDNESDAY",
124 * "closeTime": "18:00",
125 * "openDay": "WEDNESDAY",
126 * "openTime": "08:00"
127 * },
128 * {
129 * "closeDay": "THURSDAY",
130 * "closeTime": "18:00",
131 * "openDay": "THURSDAY",
132 * "openTime": "08:00"
133 * },
134 * {
135 * "closeDay": "FRIDAY ",
136 * "closeTime": "18:00",
137 * "openDay": "FRIDAY",
138 * "openTime": "08:00"
139 * }
140 * ],
141 * "specialHourPeriod": [
142 * {
143 * "comment": "Half price in the lead up to Christmas!",
144 * "endDate": "2023-12-24T23:59:00Z",
145 * "isClosed": false,
146 * "startDate": "2023-12-01T00:00:00Z"
147 * }
148 * ]
149 * },
150 * "description": "Our brand new location in London City Center!",
151 * "default": false,
152 * "email": "london@example.com",
153 * "fax": "011-44-20-12345678",
154 * "name": "London Store",
155 * "phone": "0208 123 4567",
156 * "revision": "1",
157 * "status": "ACTIVE",
158 * "timeZone": "Europe/London"
159 * }
160 */
161
Create a new location from input on the site's page

This code takes the values a user inputs into a form on the page, and uses them as the values for the properties of a new location. This page is accessible to only company managers so they can add locations to the database.

Copy Code
1/*****************************************
2 * Backend code - create-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 createNewLocation = webMethod(Permissions.Anyone, async (location) => {
10 try {
11 const elevatedCreateLocation = elevate(locations.createLocation);
12 const newLocation = await elevatedCreateLocation(location);
13
14 return newLocation;
15 } catch (error) {
16 console.error(error);
17 throw new Error(error);
18 }
19});
20
21
22/*************
23 * Page code *
24 ************/
25
26import { createNewLocation } from 'backend/create-location.web';
27
28$w.onReady(() => {
29 $w('#createLocationBtn').onClick(async () => {
30 const location = {
31 address: {
32 streetAddress: {
33 number: $w('#streetNumber').value,
34 name: $w('#streetName').value
35 },
36 city: $w('#city').value,
37 postalCode: $w('#postalCode').value,
38 },
39 description: $w('#description').value,
40 email: $w('#email').value,
41 name: $w('#name').value,
42 phone: $w('#phone').value,
43 timeZone: $w('#timeZone').value
44 };
45
46 const newLocation = await createNewLocation(location);
47 console.log('You have successfully created a new location.\n', newLocation);
48 $w('#newLocationSuccessMsg').show();
49 });
50});
51