Search...
cancelReservation( )
Developer Preview
Cancels a reservation.
Description
Sets the reservation status to CANCELED
.
Syntax
function cancelReservation(reservationId: string, revision: string, options: CancelReservationOptions): Promise<CancelReservationResponse>
cancelReservation Parameters
NAME
TYPE
DESCRIPTION
reservationId
string
Reservation ID.
revision
string
Revision number.
Include the existing revision
to prevent conflicting updates to reservations.
options
Optional
CancelReservationOptions
Options for canceling the reservation.
Returns
Return Type:
Promise<
CancelReservationResponse
>NAME
TYPE
DESCRIPTION
reservation
Reservation
Reservation.
Was this helpful?
Cancel a
WALK_IN
reservation that has no phone number (page code)Copy Code
1import { reservations } from 'wix-table-reservations.v2';23/* Sample reservationId value: "36b144c4-9cb5-4c92-96c1-5027301cac05"4 * Sample revision value: 15 */67reservations.cancelReservation(reservationId, revision)8 .then((canceledReservation) => {9 const updatedRevision = canceledReservation.revision;10 const status = canceledReservation.status;1112 console.log('Success! Canceled the reservation:', canceledReservation);13 return canceledReservation;14 })15 .catch((error) => {16 console.error(error);17 // Handle the error18 });1920/* Promise resolves to:21 * {22 * "reservation": {23 * "status": "CANCELED",24 * "source": "UNKNOWN",25 * "details": {26 * "reservationLocationId": "fab8cc1f-31cf-462f-b5bb-392594624bf2",27 * "tableIds": [],28 * "startDate": "2024-12-06T14:30:00.000Z",29 * "endDate": "2024-12-06T16:00:00.000Z",30 * "partySize": 231 * },32 * "revision": "2",33 * "migrationNotes": [],34 * "tablesWithReservationConflicts": [],35 * "_id": "36b144c4-9cb5-4c92-96c1-5027301cac05",36 * "_createdDate": "2024-01-22T11:20:02.781Z"37 * }38 * }39 */
Cancel an
ONLINE
reservation with a phone number (backend)Copy Code
1import { Permissions, webMethod } from 'wix-web-module';2import { reservations } from 'wix-table-reservations.v2';34/* Sample reservationId value: "26cdab39-9fb5-45b8-867e-5f6d85541468"5 * Sample revision value: 16 * Sample phone value: "+972555555555"7 */89export const myCancelReservationFunction = webMethod(Permissions.Anyone, async (reservationId, revision, phone) => {10 try {11 const canceledReservation = await reservations.cancelReservation(reservationId, revision, phone);1213 const updatedRevision = canceledReservation.revision;14 const status = canceledReservation.status;1516 console.log('Success! Canceled the reservation:', canceledReservation);17 return canceledReservation;18 } catch (error) {19 console.error(error);20 // Handle the error21 }2223});2425/* Promise resolves to:26 * {27 * "reservation": {28 * "status": "CANCELED",29 * "source": "UNKNOWN",30 * "details": {31 * "reservationLocationId": "fab8cc1f-31cf-462f-b5bb-392594624bf2",32 * "tableIds": [],33 * "startDate": "2024-12-06T14:30:00.000Z",34 * "endDate": "2024-12-06T16:00:00.000Z",35 * "partySize": 236 * },37 * "revision": "2",38 * "migrationNotes": [],39 * "tablesWithReservationConflicts": [],40 * "_id": "26cdab39-9fb5-45b8-867e-5f6d85541468",41 * "_createdDate": "2024-01-22T11:24:56.658Z"42 * }43 * }44 */
Close a restaurant and cancel all reservations on a given day
This scenario sets up an interface that allows a site owner to select a reservation location and a date, and then close the restaurant on that date. We start by creating a day-long special hour period during which reservations can't be made. We then cancel all reservations on that date.
Copy Code
1/* This example requires the following elements:2 * A dropdown element named `locationDropdown`.3 * A date picker element named 'datePicker`.4 * A button named `confirmButton`.5 * A text element named `cancellationNotice`.6 */78/**********************************************9 * Backend code - reservationLocations.web.js *10 **********************************************/1112import { Permissions, webMethod } from 'wix-web-module';13import { reservationLocations } from 'wix-table-reservations.v2';14import { locations } from 'wix-business-tools.v2';15import { elevate } from 'wix-auth';1617export const getRestaurantLocation = webMethod(Permissions.Anyone, async (location_id) => {18 const elevatedGetLocation = elevate(locations.getLocation);1920 try {21 const result = await elevatedGetLocation(location_id);22 return result;23 } catch (error) {24 console.error(error);25 // Handle the error26 }27});2829export const getRestautantDetails = webMethod(Permissions.Anyone, async (reservationLocationId, options) => {30 const elevatedGetReservationLocations = elevate(reservationLocations.getReservationLocation);3132 try {33 const result = await elevatedGetReservationLocations(reservationLocationId, options);34 return result;35 } catch (error) {36 console.error(error);37 // Handle the error38 }39});4041export const updateRestaurantDetails = webMethod(Permissions.Anyone, async (reservationLocationId, reservationLocationObject) => {42 const elevatedUpdateReservationLocation = elevate(reservationLocations.updateReservationLocation);4344 try {45 const result = await elevatedUpdateReservationLocation(reservationLocationId, reservationLocationObject);46 return result;47 } catch (error) {48 console.error(error);49 // Handle the error50 }51});5253/**************************************54 * Backend code - reservations.web.js *55 *************************************/5657import { Permissions, webMethod } from 'wix-web-module';58import { reservations } from 'wix-table-reservations.v2';59import { elevate } from 'wix-auth';6061export const queryReservationsInDateRange = webMethod(Permissions.Anyone, async (startDate, endDate) => {62 const elevatedQueryReservations = elevate(reservations.queryReservations);6364 try {65 const result = await elevatedQueryReservations()66 .ge('details.startDate', startDate)67 .lt('details.startDate', endDate)68 .find();69 return result;70 } catch (error) {71 console.error(error);72 // Handle the error73 }74});7576/*************77 * Page code *78 *************/7980import { reservationLocations } from 'wix-table-reservations.v2';81import { reservations } from 'wix-table-reservations.v2';8283import { getRestaurantLocation } from 'backend/reservationLocations.web';84import { getRestautantDetails } from 'backend/reservationLocations.web';85import { updateRestaurantDetails } from 'backend/reservationLocations.web';86import { queryReservationsInDateRange } from 'backend/reservations.web';8788$w.onReady(async function () {89 $w('#datePicker').hide();90 $w('#confirmButton').hide();91 $w('#cancellationNotice').hide();9293 // Set the earliest selectable date to the day after the current date94 var tomorrowsDate = new Date();95 tomorrowsDate.setDate(tomorrowsDate.getDate() + 1);96 $w('#datePicker').minDate = tomorrowsDate;97 $w('#datePicker').value = tomorrowsDate;9899 //Get the list of reservation locations100 const myReservationLocationList = await (async () => {101 try {102 let fullListObject = await reservationLocations.listReservationLocations();103 return fullListObject.reservationLocations;104 } catch (error) {105 console.error(error);106 // Handle the error107 }108 })();109110 //Create an array to hold names and values for our dropdown list111 let dropdownOptions = [];112 for (const object in myReservationLocationList) {113 //Use the _id stored in the reservation location's location object to get the location's name from the `locations` service114 const locationObject = await getRestaurantLocation(myReservationLocationList[object].location._id);115 dropdownOptions.push({116 "label": locationObject.name,117 "value": myReservationLocationList[object]._id118 })119 }120 //Populate our dropdown list with the array121 $w("#locationDropdown").options = dropdownOptions;122123 $w('#locationDropdown').onChange((event) => {124 if ($w('#locationDropdown').value) {125 $w('#datePicker').show();126 $w('#confirmButton').show();127 }128 });129130 $w('#confirmButton').onClick(async (event) => {131 const options = {132 "fieldsets": "FULL"133 }134 const selectedLocation = await getRestautantDetails($w('#locationDropdown').value, options)135136 //Create a new special hour period to push to the list in our reservation location's business schedule137 const selectedDate = $w('#datePicker').value;138 const dayAfterSelectedDate = new Date(selectedDate);139 dayAfterSelectedDate.setDate(selectedDate.getDate() + 1);140 const mySpecialHourPeriod = {141 "startDate": selectedDate,142 "endDate": dayAfterSelectedDate,143 "isClosed": true144 }145146 if (!("businessSchedule" in selectedLocation.configuration.onlineReservations)) {147 //Handle business schedule configuration148 }149 selectedLocation.configuration.onlineReservations.businessSchedule.specialHourPeriod.push(mySpecialHourPeriod);150151 await updateRestaurantDetails(selectedLocation._id, selectedLocation);152153 //Create a variable to hold of names and phone numbers of canceled reservations.154 let canceledReservationsNote = "Notify of cancellation:\n";155156 //Query for reservations on the selected date and cancel any at our selected reservation location157 let reservationsOnSelectedDate = await queryReservationsInDateRange(selectedDate, dayAfterSelectedDate);158 for (let reservation in reservationsOnSelectedDate._items) {159 if (reservationsOnSelectedDate._items[reservation].details.reservationLocationId == selectedLocation._id) {160 let options = {161 "phone": reservationsOnSelectedDate._items[reservation].reservee.phone162 }163164 const canceledReservation = await (async () => {165 try {166 return reservations.cancelReservation(reservationsOnSelectedDate._items[reservation]._id, reservationsOnSelectedDate._items[reservation].revision, options)167 } catch (error) {168 console.error(error);169 // Handle the error170 return false;171 }172 })();173174 if (canceledReservation) {175 canceledReservationsNote += "First name: " + reservationsOnSelectedDate._items[reservation].reservee.firstName +176 ". Last name: " + reservationsOnSelectedDate._items[reservation].reservee.lastName +177 ". Phone: " + reservationsOnSelectedDate._items[reservation].reservee.phone + "\n";178 }179 }180 }181182 $w('#cancellationNotice').text = canceledReservationsNote;183 $w('#cancellationNotice').show();184 });185});186