createLocation( )
Creates a location.
Description
The createLocation()
function returns a Promise that resolves to the created location.
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 to create.
Returns
Created location.
Return Type:
NAME
TYPE
DESCRIPTION
Location ID.
Address.
Whether the location is archived. Archived locations can't be updated.
Note: Archiving a location
doesn't affect its status
.
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.
Whether this is the default location. There can only be one default location per site. The default location can't be archived.
Location description.
Email address.
Fax number.
Location type.
Supported values: BRANCH
, OFFICES
, RECEPTION
, HEADQUARTERS
.
Note: This field is currently not supported.
Location name.
Phone number.
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.
Location status.
Supported values: ACTIVE
, INACTIVE
.
Default: ACTIVE
Notes:
doesn't affect the location's status.
INACTIVE
status is currently not supported.
Timezone in America/New_York
format.
Was this helpful?
1import { locations } from 'wix-business-tools.v2';2import { elevate } from 'wix-auth';34/* 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 */1314export 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);1920 return newLocation;21 } catch (error) {22 console.error(error);23 // Handle the error24 }25}2627/* 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
1import { Permissions, webMethod } from 'wix-web-module';2import { locations } from 'wix-business-tools.v2';3import { elevate } from 'wix-auth';45/* 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 */1415export 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);2021 return newLocation;22 } catch (error) {23 console.error(error);24 // Handle the error25 }26});2728/* 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
1import { Permissions, webMethod } from 'wix-web-module';2import { locations } from 'wix-business-tools.v2';3import { elevate } from 'wix-auth';45/* 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.06860014 * },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 */7475export 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);8081 return newLocation;82 } catch (error) {83 console.error(error);84 // Handle the error85 }86});8788/* 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.068698 * },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
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.
1/*****************************************2 * Backend code - create-location.web.js *3 ****************************************/45import { Permissions, webMethod } from 'wix-web-module';6import { locations } from 'wix-business-tools.v2';7import { elevate } from 'wix-auth';89export const createNewLocation = webMethod(Permissions.Anyone, async (location) => {10 try {11 const elevatedCreateLocation = elevate(locations.createLocation);12 const newLocation = await elevatedCreateLocation(location);1314 return newLocation;15 } catch (error) {16 console.error(error);17 throw new Error(error);18 }19});202122/*************23 * Page code *24 ************/2526import { createNewLocation } from 'backend/create-location.web';2728$w.onReady(() => {29 $w('#createLocationBtn').onClick(async () => {30 const location = {31 address: {32 streetAddress: {33 number: $w('#streetNumber').value,34 name: $w('#streetName').value35 },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').value44 };4546 const newLocation = await createNewLocation(location);47 console.log('You have successfully created a new location.\n', newLocation);48 $w('#newLocationSuccessMsg').show();49 });50});51