Change Email Notification Logic

Ok folks so here is my current process;

Vistor to the website goes to the appointment page and makes an appointment, the appointment is always free (no payment gateway) and the booking shows up in the admin with No service provider allocated with status as Pending.

I as admin then allocate the service provider in the admin area and leave the status as pending and save changes. (At this point I want it to send an email to the Service Provider Only which has the details of the send_confirmation email $body but with a different subject field to that set in the admin area settings)

The service provider then visits the site, logs into the front end and from the front end Confirms the appointment changing the status automatically to confirmed and the send_confirmation function is then called and sends the customer details of the appointment now that it is confirmed. (This already happens however it duplicates the email to the service provider again)

What code do I need to add to the appointments.php to achieve the first bit of just sending the send_confirmation email to the service provider only when they are allocated the appointment from the admin and that change is saved?

Secondly - what code do I need to remove to prevent the duplicate email being sent to the service provider when they have confirmed the booking as they do not need this second email?

I do not want the send_notification to be used as it does not contain the details of the appointment, hence choosing the send_confirmation template on both occasions.

So three stages;

Appointment made by customer: Front End: Appointment not allocated to a service provider and remains in status Pending

Admin then allocate to service provider in the admin area leaving status as pending

Service provider then confirms the appointment front end changing status to confirmed.

Many Thanks

Wayne

  • Saurabh

    Hi Wayne,

    Hope you are doing well!

    What code do I need to add to the appointments.php to achieve the first bit of just sending the send_confirmation email to the service provider only when they are allocated the appointment from the admin and that change is saved?

    You'd need to add the following code to appointments.php or maybe a new plugin or in your theme's functions.php:

    add_action('app-appointment-inline_edit-after_save', 'incsub_custom_notification_emails');
    
    function incsub_custom_notification_emails ($app_id, $data){
    
    	$worker = $data['worker'];
    	$status = $data['status'];
    
    	// status is still pending but a worker has been assigned
    	if( 'pending' === $status && !empty($worker)){
    		global $appointments;
    
    		// set up the email body from the confirmation message template
    		$body = $appointments->add_cancel_link(
    				$appointments->_replace(
    					$appointments->options["confirmation_message"],
    					$data['name'],
    					$appointments->get_service_name( $data['service']),
    					$appointments->get_worker_name( $data['worker']),
    					$data['start'],
    					$data['price'],
    					$appointments->get_deposit($data['price']),
    					$data['phone'],
    					$data['note'],
    					$data['address'],
    					$data['email'],
    					$data['city']
    				),
    				$app_id
    			);
    		$worker_email = $appointments->get_worker_email( $worker );
    
    		// add your subject here
    		$subject = '';
    
    		// send the mail
    		wp_mail(
    			$worker_email,
    			$subject,
    			$body,
    			$appointments->message_headers();
    			);
    
    	}
    
    }

    Secondly - what code do I need to remove to prevent the duplicate email being sent to the service provider when they have confirmed the booking as they do not need this second email?

    For this you'd have to modify your appointments.php on about line 4344, and comment out the following code:

    $worker_email = $this->get_worker_email( $r->worker );
    if ( $worker_email )
        $to[]= $worker_email;

    to:

    // $worker_email = $this->get_worker_email( $r->worker );
    // if ( $worker_email )
    //     $to[]= $worker_email;

    This will prevent a copy of the confirmation from being sent to the service provider.

    Best

    Saurabh

  • Wayne

    Hi @Saurabh Shukla

    Ok I added a new plugin folder called appontment-emaillogic and created a php file called emaillogic.php

    I added the code above with the php tags and it came up with a syntax error on line 42 of your example;

    $appointments->message_headers();

    I changed this to

    $appointments->message_headers()

    So I changed it to this which allowed the plugin to activate however it does not work when I have a new appointment and I allocate a service provider leaving the status as pending and click Save Changes.

    <?php
    
    /*
    Plugin Name: Email on allocation
    Description: Sends email whilst status is pending and official allocated.
    Plugin URI: https://premium.wpmudev.org/project/appointments-plus/
    Version: 1.0
    AddonType: Users
    Author: Saurabh Shukla (Incsub)
    */
    
    add_action('app-appointment-inline_edit-after_save', 'incsub_custom_notification_emails');
    
    function incsub_custom_notification_emails ($app_id, $data){
    
    	$worker = $data['worker'];
    	$status = $data['status'];
    
    	// status is still pending but a worker has been assigned
    	if( 'pending' === $status && !empty($worker)){
    		global $appointments;
    
    		// set up the email body from the confirmation message template
    		$body = $appointments->add_cancel_link(
    				$appointments->_replace(
    					$appointments->options["confirmation_message"],
    					$data['name'],
    					$appointments->get_service_name( $data['service']),
    					$appointments->get_worker_name( $data['worker']),
    					$data['start'],
    					$data['price'],
    					$appointments->get_deposit($data['price']),
    					$data['phone'],
    					$data['note'],
    					$data['address'],
    					$data['email'],
    					$data['city']
    				),
    				$app_id
    			);
    		$worker_email = $appointments->get_worker_email( $worker );
    
    		// add your subject here
    		$subject = '';
    
    		// send the mail
    		wp_mail(
    			$worker_email,
    			$subject,
    			$body,
    			$appointments->message_headers()
    			);
    
    	}
    
    }
    
    ?>

    I have commented out those fields to prevent the duplicate email going to the service provider which seems to stop that from happening so its just getting this other bit working now.

    As always support access is granted.

    Regards

    Wayne

  • Saurabh

    Hi Wayne,

    I apologise for the inconvenience. There was an error in the code. I have replaced the erroneous code:

    add_action('app-appointment-inline_edit-after_save', 'incsub_custom_notification_emails');

    to

    add_action('app-appointment-inline_edit-after_save', 'incsub_custom_notification_emails', 99, 2);

    Without the 2 there, our custom function won't get the data passed and the actual mailing won't work.

    With this fix, the mails will work, as expected.

    Do let us know if there are any further issues with this.

    Best

    Saurabh

  • Wayne

    Hi @Saurabh Shukla

    Please see this https://premium.wpmudev.org/forums/topic/extending-email-shortcodes-to-allow-status-to-be-displayed?replies=8#post-684227

    To explain further issues as a result of testing today.

    When the appointment is created the confirmation email sent to the person booking the appointment $body shows as follows;

    Kick Off Time: 27 June, 2014 11:00 am
    Home Team: Wakefield Wildcats
    Away Team: Leeds Rhinos
    Age Group: Open Age
    Competition: Super League
    Location: Wakefield

    Here Is The Current Status
    Match Official Allocated: Not Appointed
    Current Status: STATUS

    Note the only bit that is not being replaced is STATUS - which at this stage should be translated to "pending"

    When I then assign a service provider to this your code above now sends the confirmation email to the service provider which is perfect however most of my custom fields do not translate because the same section of the email now looks like this;

    Here Are The Appointment Details

    Kick Off Time: 27 June, 2014 11:00 am
    Home Team: FIELD_HOMETEAM
    Away Team: FIELD_AWAYTEAM
    Age Group: FIELD_AGEGROUP
    Competition: FIELD_COMPETITION
    Location: FIELD_LOCATIONOFGROUND

    Here Is The Current Status
    Match Official Allocated: Wayne Baker
    Current Status: STATUS

    Again the STATUS along with all other custom fields do not translate.

    When the service provider confirms the appointment the confirmation email is sent back to the customer which is perfect but again the STATUS is not showing and the same section of the email looks like this;

    Here Are The Appointment Details

    Kick Off Time: 27 June, 2014 11:00 am
    Home Team: Wakefield Wildcats
    Away Team: Leeds Rhinos
    Age Group: Open Age
    Competition: Super League
    Location: Wakefield

    Here Is The Current Status
    Match Official Allocated: Wayne Baker
    Current Status: STATUS

    Look forward to any assistance or advice to try and resolve this and the related topic.

    https://premium.wpmudev.org/forums/topic/extending-email-shortcodes-to-allow-status-to-be-displayed

    Many Thanks

    Wayne

  • Saurabh

    Hi Wayne,

    Sorry for the inconvenience! We should've added a filter that the fields addon and @Vaughan's code on the other thread use to replace those additional fields in the email template. I've modified the code again:

    <?php
    
    /*
    Plugin Name: Email on allocation
    Description: Sends email whilst status is pending and official allocated.
    Plugin URI: https://premium.wpmudev.org/project/appointments-plus/
    Version: 1.0
    AddonType: Users
    Author: Saurabh Shukla (Incsub)
    */
    
    add_action('app-appointment-inline_edit-after_save', 'incsub_custom_notification_emails', 99, 2);
    
    function incsub_custom_notification_emails ($app_id, $data){
    
    	// nothing's changed!
    	if(!$app_id){
    		return;
    	}
    
    	$worker = $data['worker'];
    	$status = $data['status'];
    
    	// status is still pending but a worker has been assigned
    	if( 'pending' === $status && !empty($worker)){
    		global $appointments;
    
    		// set up the email body from the confirmation message template
    		$body = $appointments->_replace(
    					$appointments->options["confirmation_message"],
    					$data['name'],
    					$appointments->get_service_name( $data['service']),
    					$appointments->get_worker_name( $data['worker']),
    					$data['start'],
    					$data['price'],
    					$appointments->get_deposit($data['price']),
    					$data['phone'],
    					$data['note'],
    					$data['address'],
    					$data['email'],
    					$data['city']
    				);
    		$body = apply_filters( 'app_confirmation_message', $appointments->add_cancel_link($body, $app_id), $data, $app_id);
    
    		$worker_email = $appointments->get_worker_email( $worker );
    
    		// add your subject here
    		$subject = 'You Are Appointed As Referee - PLEASE CONFIRM';
    
    		// send the mail
    		wp_mail(
    			$worker_email,
    			$subject,
    			$body,
    			$appointments->message_headers()
    			);
    
    	}
    
    }
    ?>

    I have already made the necessary changes on your site, so you won't have to, again. This is pasted here for reference.

    Best

    Saurabh

  • Wayne

    Hi @Saurabh Shukla

    Many thanks for your efforts and most is now working. The last slight issue is when the service provider confirms the appointment via their profile the STATUS field still does not get replaced with the word 'confirmed'

    It does replace it when i assign a service provider and leave the status as pending, however when confirmed it does not and the email looks like this;

    Kick Off Time: 27 June, 2014 11:00 am
    Home Team: Wakefield Wildcats
    Away Team: Leeds Rhinos
    Age Group: Open Age
    Competition: Super League
    Location: Wakefield

    Here Is The Current Status
    Match Official Allocated: Not Appointed
    Current Status: STATUS

    Regards

    Wayne

  • Saurabh

    Hi Wayne,

    This will need a little more modification than I had assumed, till now.

    First, in our custom plugin, we'd have to change the name of the filter, so it doesn't conflict with the one in Appointments+. This line:

    $body = apply_filters( 'app_confirmation_message', $appointments->add_cancel_link($body, $app_id), $data, $app_id);

    will change to:

    $body = apply_filters( 'app_worker_confirmation_message', $appointments->add_cancel_link($body, $app_id), $data, $app_id);

    In the code mentioned by @Vaughan in the other thread, you'd have to add a line:

    add_filter('app_worker_confirmation_message', 'my_app_expand_status', 10, 2);

    Then, in plugins/appointments/includes/addons/app-users-additional_fields.php at about line 52, add this line, just under // Email filters, add this line:

    add_filter('app_worker_confirmation_message', array($this, 'expand_email_macros'), 10, 3);

    I have made these changes. This will solve all the issues, hopefully. Do test this and let us know.

    Best

    Saurabh

  • Saurabh

    Hi @Wayne,

    Hope you are well today.

    Just to clarify, the last changes I had suggested were not it. The problem was with the function in the other thread, which didn't recognise these mods. I have fixed it in that thread, as well.

    For anyone else looking for this solution, here's the final code:

    <?php
    
    /*
    Plugin Name: Email on allocation
    Description: Sends email whilst status is pending and official allocated.
    Plugin URI: https://premium.wpmudev.org/project/appointments-plus/
    Version: 1.0
    AddonType: Users
    Author: Saurabh Shukla (Incsub)
    */
    
    add_action('app-appointment-inline_edit-after_save', 'incsub_custom_notification_emails', 99, 2);
    
    function incsub_custom_notification_emails ($app_id, $data){
    
    	// nothing's changed!
    	if(!$app_id){
    		return;
    	}
    
    	$worker = $data['worker'];
    	$status = $data['status'];
    
    	// status is still pending but a worker has been assigned
    	if( 'pending' === $status && !empty($worker)){
    		global $appointments;
    
    		// set up the email body from the confirmation message template
    		$body = $appointments->_replace(
    					$appointments->options["confirmation_message"],
    					$data['name'],
    					$appointments->get_service_name( $data['service']),
    					$appointments->get_worker_name( $data['worker']),
    					$data['start'],
    					$data['price'],
    					$appointments->get_deposit($data['price']),
    					$data['phone'],
    					$data['note'],
    					$data['address'],
    					$data['email'],
    					$data['city']
    				);
    		$body = apply_filters( 'app_confirmation_message', $appointments->add_cancel_link($body, $app_id), $data, $app_id);
    
    		$worker_email = $appointments->get_worker_email( $worker );
    
    		// add your subject here
    		$subject = 'You Are Appointed As Referee - PLEASE CONFIRM';
    
    		// send the mail
    		wp_mail(
    			$worker_email,
    			$subject,
    			$body,
    			$appointments->message_headers()
    			);
    
    	}
    
    }
    ?>

    Best

    Saurabh

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.