Deleting Booking When External Calendar Entry Is Deleted
This tutorial explains how to delete or cancel a booking when an external calendar entry is deleted, using the example of the iCal synchronization between Google Calendar and JetBooking on the WordPress website.
Before you start, check the tutorial requirements:
- JetBooking plugin installed and activated (at least 3.7.1 version) with the iCal synchronization set.
If you haven’t done it yet, check this tutorial.
When synchronizing external calendars for apartments, a new booking item is created. However, when we modify the external calendar by deleting the entry and re-synchronizing, it remains in the booking list on the site; therefore, this date range cannot be booked.
To delete or update the booking status to one that allows you to use the re-selected dates, follow these steps.
Create an Additional Booking Column
To create an additional custom column, go to the WordPress Dashboard > Bookings > Settings > Tools tab, press the “New DB Column” button, and enter a unique Column name (e.g., “_calendar_id”). Then, push the “Update Booking Tables” button to save the changes. The column will be used to save information about the external calendar and in queries.
Add Hooks
After creating the custom column to store calendar information, use the jet-booking/ical/import/node hook. This hook runs for each booking during iCal synchronization and allows you to attach the calendar ID (a unique iCal link) to each imported booking.
Since a single apartment can be synced with multiple external calendars, this ID helps distinguish which bookings come from which source, making it easier to manage various calendar integrations per unit.
The hook can be added either via a custom code snippets plugin (like Code Snippets) or directly in your theme’s functions.php file (preferably a child theme to prevent overwrite on updates).
For example, we install and activate the Code Snippets plugin, then proceed to the WordPress Dashboard > Snippets tab and click the “Add New” button.
We type the hook name into the title bar (e.g., “JetBooking iCal Calendar ID Hook”) and enter such code into the Functions (PHP) textarea:
add_filter( 'jet-booking/ical/import/node', function( $import_node, $node, $calendar_object, $calendar_url ) {
$import_node['_calendar_id'] = esc_url( $calendar_url ); // `_calendar_id` name of custom created DB column via plugin settings.
return $import_node;
}, 10, 4 );
Now, press the “Save Changes and Activate” button to save the snippet.
For final processing, we’ll use the jet-booking/ical/import/log hook. While not originally intended for this use case, it fits well due to its execution timing and available parameters. The hook runs once per calendar during synchronization.
Using it, we can fetch all bookings tied to the current calendar ID and compare them to the latest iCal data. Any bookings no longer present in the external calendar can then be marked as canceled.
Push the “Add New” button one more time, type the title (e.g., “Cancel iCal Bookings by Calendar ID (JetBooking Hook)”), and enter the following code:
add_filter( 'jet-booking/ical/import/log', function( $import_log, $post_id, $unit_id, $inserted, $skipped, $calendar_url ) {
// Get all bookings that are related to the synced calendar.
$calendar_bookings = jet_abaf_get_bookings( [
'meta_query' => [
[
'column' => '_calendar_id', // `_calendar_id` name of custom created DB column via plugin settings.
'operator' => '=',
'value' => $calendar_url
],
],
'return' => 'arrays',
] );
if ( empty( $calendar_bookings ) ) {
return $import_log;
}
foreach ( $calendar_bookings as $booking ) {
// Compare synced bookings to existing.
if ( in_array( $booking['ID'], $inserted ) || in_array( $booking['import_id'], $skipped ) ) {
continue;
}
// Change status to cancelled for calendar bookings that are not in sync anymore.
jet_abaf()->db->update_booking( $booking['ID'], [ 'status' => 'cancelled' ] );
}
return $import_log;
}, 10, 6 );
If you need to delete the reservation from the site completely, then the line:
jet_abaf()->db->update_booking( $booking['ID'], [ 'status' => 'cancelled' ] );
can be replaced with:
jet_abaf()->db->delete_booking( [ 'booking_id' => $booking['ID'] ] );
Check the Result
In this case, we set the hook to change the booking status to “Cancelled” after deleting a booking from the external calendar.
First, we created several bookings in the Google Calendar.
Then, we synchronize two calendars in the WordPress Dashboard > Bookings > Calendars tab by clicking the “Synch” button for the required apartment.
After synchronization on the WordPress website, they are displayed in the WordPress Dashboard > Bookings tab.
Then, we return to the Google Calendar and delete one booking.
After the synchronization, its status was changed to “Cancelled” on the WordPress website.
That’s all. Now you know how to delete or cancel a booking when an external calendar entry is deleted, using the example of the iCal synchronization between Google Calendar and JetBooking on the WordPress website.