Help Center
Deleting Booking When External Calendar Entry Is Deleted

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.

Things to know

In this tutorial, we use Google Calendar as an example; however, the same workflow applies to other calendar services as well.

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.

Warning

Before proceeding with the tutorial′s steps, create a backup or replicate the actions on the staging site. The backup ensures the website restoration if an error occurs. To learn more, proceed to the How to Backup and Restore a WordPress Website with a Plugin article.

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.

Warning

Be aware that the Hide DB columns manager toggle in the Advanced settings tab hides both columns and the “New DB Column” button. Ensure it is turned off when creating columns.

additional booking table column for external calendar

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 );
Warning

Ensure to replace the _calendar_id with your JetBooking DB column name set in the previous step.

JetBooking iCal Calendar ID Hook
Things to know

Note that the calendar ID is added only to newly imported bookings. Existing bookings synchronized earlier will not have this ID assigned.

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'] ] );
Warning

Once again, ensure to replace the _calendar_id with your JetBooking DB column name set in the previous step.

Cancel iCal Bookings by Calendar ID (JetBooking Hook)

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.

bookings in Google Calendar

Then, we synchronize two calendars in the WordPress Dashboard > Bookings > Calendars tab by clicking the “Synch” button for the required apartment.

synchronizing JetBooking and Google Calendar

After synchronization on the WordPress website, they are displayed in the WordPress Dashboard > Bookings tab.

bookings created from Google Calendar

Then, we return to the Google Calendar and delete one booking.

deleting a booking from Google Calendar

After the synchronization, its status was changed to “Cancelled” on the WordPress website.

booking gets the cancelled status after being deleted from the Google Calendar

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.

Was this article helpful?
YesNo

Need help?

Ask Facebook community

Get answers from 30K+ Crocoblock Community experts from all over the world.

Start a live chat

Ask question to support agent and share its details via chat messages.