How to guide: Add are you sure? page and cut off time for client cancellations

I'm in the process of learning some PHP and I put this together for my own site. I thought I would share here in case someone else might find it useful. This involves a bit of under the hood tinkering with wordpress so probably not suitable for absolute beginners but hopefully it's not too difficult to follow.

There were two problems with the plugin that I wanted to solve:

Firstly I regularly have customers who click on the cancellation link by accident and cancel their appointment, sometimes without even realising it. This happens often enough that I always have to call people and check they really meant to cancel. My second problem is I sometimes have customers who will cancel an appointment right at the last minute, giving me no notice to fill their slot. I think because all they have to do is click a link they don't feel bad, if they had to call up maybe they wouldn't do it.

My goal was to get the cancellation link in the email to redirect to a new page on my site which informed the client they are about to cancel their appointment and requires them to click a confirmation button before the appointment is cancelled. In addition to this I wanted to check the start time of their appointment and if there are less than say 48 hours to go inform them that it is too late to cancel online and that they have to call the office.

My implementation of this involves modifying the functions.php file for the plugin so make a copy of the original first and be aware that app updates will require you to apply the modified code again. There might be a way of installing this as an muplugin or something which doesn't involve changing the plugin code, but my PHP isn't good enough yet to figure that out. You also need to install a plugin to allow you to insert php into your wordpress pages, I use this one:

https://wordpress.org/support/view/plugin-reviews/insert-php

Now in your wordpress admin menu choose plugins -> editor and select the functions.php file of appointments. Search for the following bit of code:

/* Cancel by the link in email */
			// We don't want to break any other plugin's init, so these conditions are very strict
			if ( isset( $_GET['app_cancel'] ) && isset( $_GET['app_id'] ) && isset( $_GET['app_nonce'] ) ) {
				$app_id = $_GET['app_id'];
				$app = appointments_get_appointment( $app_id );

				if( isset( $app->status ) )
					$stat = $app->status;
				else
					$stat = '';

and replace it with this, you need to replace the first $myurl variable with the URL of the new page on your site which will handle the cancellation (see below):

/* Cancel by the link in email */
			// We don't want to break any other plugin's init, so these conditions are very strict
			if ( isset( $_GET['app_cancel'] ) && isset( $_GET['app_id'] ) && isset( $_GET['app_nonce'] ) && !isset( $_GET['confirm'] ) && !isset( $_GET['sure'] ) ) {
				$myurl = 'https://www.whatever.com/booking/cancel'; /* Change this to match the URL of your are you sure you want to cancel page */
				$myurl = '$myurl . '?' . $_SERVER['QUERY_STRING'] . '&sure=yes';
				wp_redirect($myurl);
				exit();
}
			if ( isset( $_GET['app_cancel'] ) && isset( $_GET['app_id'] ) && isset( $_GET['app_nonce'] ) && isset( $_GET['sure'] ) && isset( $_GET['confirm'] ) ) {
				$app_id = $_GET['app_id'];
				$app = appointments_get_appointment( $app_id );

				if( isset( $app->status ) )
					$stat = $app->status;
				else
					$stat = '';

Now you need to make the page which will handle the cancellation. The insert php plugin is essential for this to work. Heres the basic code you need, just paste this into the text editor of your new wordpress page, add your own content in the relevant sections and choose the minimum cancellation time you will accept:

[insert_php]
global $wpdb;
$cutoffpoint = 48; /* Set 48 to the minimum number of hours before the appointment you will allow client to cancel */
$appid = $_GET['app_id'];
$details = $wpdb->get_row( "SELECT name, start FROM wp_app_appointments WHERE ID = '$appid'" );
$starttime = strtotime($details->start);
$name = $details->name;
$difference = ($starttime - time());
$cutoff = ($cutoffpoint * 3600);

if ($difference >= $cutoff) {
$myurl = $_SERVER['HTTP_REFERER'] . '' . $_SERVER['REQUEST_URI'] . '&confirm=yes';

/* Whatever you put here will be displayed if the appointment is far enough into the future that the client can still cancel. This example displays an image which when clicked will cancel the appointment. */

echo &quot;<h2>You are about to cancel your booking, are you sure $name?</h2><br><br><br><a href='$myurl'><img src='https://www.yourwebsite.com/images/confirm.png' alt='Cancel your booking' width='270' height='98' /></a>&quot;; }
else

/* Whatever you put here will be displayed if it is too close to the appointment time for your client to cancel */

 {
echo &quot;<h2>Sorry $name, your appointment can no longer be cancelled online</h2><br><br><br>As its less than $cutoffpoint hours until your appointment we are unable to process an online cancellation. Please call the office on 09876 54 32 10.&quot;;}
[/insert_php]

Thats it, here's a couple of screenshots of how it looks on my site

  • Adam Czajczyk

    Hello Michael,

    I hope you're well today and thank you for posting!

    That's a great addition to the community and you did a lot of work to find out about all this and make it work as expected. Thank you for sharing this with us and I'm sure some other members of the community will find that very helpful.

    My two remarks on this though would be:

    - indeed making changes to plugin's core files is not advised and may cause some issues in future, especially if an update brings significant changes so maybe it would be a good idea to find a way to redirect without changing the code. Yet, I agree that there are cases when this is not possible and also - let me emphasize that again out loud - you did great job!

    - instead of adding PHP to a post however I'd suggest simply creating a custom page template and putting all the code in a template file and then assigning that template to a page; that'd be much safer and also WP Codex-compliant solution and should have the same effect :slight_smile:

    Having said that, I'd like to thank you again for adding up to a community such a great way!

    Thanks and keep up a good work!

    Have a nice day,
    Adam

  • Michael

    Hi Adam,

    Thanks for the feedback and the tip. I hadn't thought of adding the code to a page template, but now you say it then it seems so obvious. Is there somewhere specific within the template file to add the php or will it still work if I drop it inside the HTML tags?

    I've put together a few custom add ons for Appointments+ to add features I wanted, and I guess many other members probably have too. It might be nice if WPMU had a separate repository where people could share their code.

    Regards,

    Michael

  • Predrag Dubajic

    Hi Michael,

    Is there somewhere specific within the template file to add the php or will it still work if I drop it inside the HTML tags?

    Depends on where you want the code to show, you can simply add it before or after the_content() function if you still want to be able to add the content from back end, or if you're only looking for a way to show your code then you can simply replace the_content() with your code.
    You can use your theme default page template as a place to pull correct HTML holders from, and I have attached below example template with your code to help out a bit with setting it up :slight_smile:

    I've put together a few custom add ons for Appointments+ to add features I wanted, and I guess many other members probably have too. It might be nice if WPMU had a separate repository where people could share their code.

    We had few suggestions for this and are actually considering adding something like this.
    For now you can share your code just as you did now, by opening new thread, I myself bookmark these kind of pages and forward to users searching for similar things :slight_smile:

    Best regards,
    Predrag

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.