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

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 */
Create a new location

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