Appointments+ hook/action for Completed Appointment

Hello,

I would like the system to auto send an email each time an appointment is marked completed. Currently, this is not built into the plugin so I need to add it in.

Is there a hook/action that does this? If so, where can I find it so I can start customizing what happens after that.

Any help is appreciated,

Thanks,
Judah

  • Vaughan

    Hi Judah,

    Hope you're well?

    I've been looking at the code, but I can't see a hook on the completed status, I thought maybe hooking into one of the notification functions, but not sure on that as I don't think there's a trigger for it on the completed status change, let me just ask one of our developers to see if he can come up with a quick solution for you. Hopefully he should respond shortly, but could take a little longer than usual depending on his workload.

    Hope this helps

  • Judah

    Hey @Hoang Ngo,

    This works great!

    add_action('app_remove_expired','your_function',10,2);
    function your_function($appointment,$new_status){
    if($new_status=='completed'){
    $title = "Appointment Completed";
    $message = "Dear CLIENT, Thank you for your time!";
    wp_mail( $appointment->email, wp_specialchars_decode( $title ), $message );
    }
    }

    I would like to build out on this if possible?

    1. How can get the email to recognize the word "CLIENT" and replace it with the clients name? If possible, the other fields that are recognized by the usual email in the appointments settings.

    2. How to send this to the Service Provider and Admin? - (Mainly just the Service Providers) - As of now, the email only gets sent to the Client.

    If you have any suggestions that would be great!

    Thanks,
    Judah

  • Judah

    After some further testing, I noticed that this email was sent within 1 minute of the appointment time. Ex: The appointment was for 6:00 - 6:30. The email was send at 6:01.

    I am assuming that the hook 'app_remove_expired' fires as soon as the starting time for that appointment passes (ie 6:00:01)

    Is there a way to either have the hook fire at the time the lesson end (ie 6:30)?

    If not, can we put a delay on the function to fire (ex: after 30 minutes) ?

  • Hoang Ngo

    Hi @ Judah,

    Yes, but we will need to modify the code a little bit.

    In the file appointments.php, line 4827, you will see a code block like this
    $expireds = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$this->app_table} WHERE start<%s AND status NOT IN ('completed', 'removed')", date("Y-m-d H:i:s", $this->local_time)) );
    Simply change it to:
    $expireds = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$this->app_table} WHERE end<%s AND status NOT IN ('completed', 'removed')", date("Y-m-d H:i:s", $this->local_time)) );
    Let's see if it help :slight_smile:

    Best regards,
    Hoang

  • Judah

    Hey @Hoang Ngo,

    I forgot to ask.. for the below code if I want to do three things.

    1. Include CLIENT in the "stuff go here" part and have it pull the client name - how can I do that?

    2. How can I implement line breaks in the message text

    end of sentence. <br/ > New Sentence..

    3. How can I implement a link

    < a href="example.com">link text</a> more text.

    Here is the code you provided earlier:

    add_action('app_remove_expired','your_function',10,2);
    function your_function($appointment,$new_status){
    if($new_status=='completed'){
    //stuffs go here
    }
    }

  • Hoang Ngo

    Hi again,

    1. Include CLIENT in the "stuff go here" part and have it pull the client name - how can I do that?

    You can get client name by $appointment->name. Example

    add_action('app_remove_expired','your_function',10,2);
    function your_function($appointment,$new_status){
    if($new_status=='completed'){
    $name =$appointment->name;
    }
    }

    2. How can I implement line breaks in the message text

    Using
    (not <br/ >, it might broken) tag is right, or you can wrap your sentence in <p></p> tag, like this

    <p>Some text here</p>
    <p>Another text in new line here</p>

    < a href="example.com">link text

    The tag should be link text (not < a)

    Hope this will help :slight_smile:

    Best regards,
    Hoang

  • Judah

    Hey Hoang,

    I am trying to use the below code, but it is not working. (once a lesson goes from confirmed -> complete, no emails get sent to any users)

    add_action('app_remove_expired','appointment_completed_notifications');
    function appointment_completed_notifications($appointment,$new_status){
    if($new_status=='completed'){
    $name = $appointment->name;
    $title = "How was your Appointment?";
    $message = "<p>Dear ".$name.",</p> <p>Thank you for coming! We would like to hear your feedback on your recent visit.
    Was your technician helpful?</p>
    <p>Enter Feedback Here</p>";
    wp_mail( $appointment->email, wp_specialchars_decode( $title ), $message );
    }
    }

    I removed the original "10,2" from the code you provided me, as it was breaking my site.

    I changed my appointments.php line 4827 to
    $expireds = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$this->app_table} WHERE end<%s AND status NOT IN ('completed', 'removed')", date("Y-m-d H:i:s", $this->local_time)) );

    Any Idea on what is causing my code not to fire?

  • Hoang Ngo

    Hi Judah ,

    I hope you are well today.

    I removed the original "10,2" from the code you provided me, as it was breaking my site.

    Hmm, what's the error you got when append the 10,2, as without that, this will not work. The 10 is the priority of the hook, and 2 is the number of parameters passing to our hook function (this case it is $appointment and $new_status). If you remove the 10,2, WordPress hook system will understand you only want to pass 1 parameter ($appointment), and your function will not work.

    So for now, I will need to know the error you got when using the "10,2", and we will sort this out :slight_smile:

    Best regards,
    Hoang

  • Judah

    Hey @Hoang Ngo,

    The system never sent the email :slight_frown:

    It could have something to do with either of the below "changes"

    1. When we changed the appointments.php file. Line 4827 $expireds = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$this->app_table} WHERE end<%s AND status NOT IN ('completed', 'removed')", date("Y-m-d H:i:s", $this->local_time)) );

    2. When we added in html to the function (a href, br & p tags).

    I have changed the appointments.php line back to the original code (as it was working - but was sending the email as soon as the appointment started) that was there and in about 37 minutes I will know which one of the above two changes are causing the email not to send.

    Will keep you updated.

    Thanks,
    Judah

  • Judah

    Hey @Hoang Ngo,

    So it didn't work :slight_frown:

    I did not receive any emails when the appointment changed status from confirmed to complete.

    I have your mu plugin installed and have the appointments.php line 4827 set to:
    $expireds = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$this->app_table} WHERE start<%s AND status NOT IN ('completed', 'removed')", date("Y-m-d H:i:s", $this->local_time)) );

    Any ideas?

  • Judah

    Hey @Hoang Ngo,

    Good news and bad news!

    First off, I did not enter the most recent code you suggested (for email testing).

    Going back to the original topic/code - I did receive an email at 22:08 on 1/23/15!

    This is the only code I have regarding this topic.
    '<?php
    add_action('app_remove_expired', 'appointment_completed_notifications', 10, 2);
    function appointment_completed_notifications($appointment, $new_status)
    {
    if ($new_status == 'completed') {
    $name = $appointment->name;
    $title = "How was your Appointment?";
    $message = "<p>Dear " . $name . ",</p> <p>Thank you for coming! We would like to hear your feedback on your recent visit.
    Was your technician helpful?</p>
    <p>Enter Feedback Here</p>";
    wp_mail($appointment->email, wp_specialchars_decode($title), $message);
    }
    }'

    1. The appointment times I had booked for that user, on that day were: 9, 10, 11, 12 & 22.. So I am unsure which appointment that was from (I am assuming from the one for 22 as that was the soonest one after I "reset" my code to your original)

    2. It was sent about 8 minutes after the appointment began. I am trying to have it be sent 55 minutes (my appointments are 55 minutes in length.

    3. The $name feature worked!

    4. I will have some more appointments set up for tonight to test if it will be sent to the same user each time a lesson ends.

    Any ideas on how to fix item 2 from the list above?

    If you need me to test the email function I can add it to my functions.php file and let you know the result but I am not having any other issues with email being sent.

    Will keep you updated on item #4.

    Thanks,
    Judah

  • Hoang Ngo

    Hi @ Judah,

    So I understand now you are using the original query
    $expireds = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$this->app_table} WHERE start<%s AND status NOT IN ('completed', 'removed')", date("Y-m-d H:i:s", $this->local_time)) );
    I've checked the code again, for now, the plugin using 10 minutes interval to check appointments status and update it if necessary. Changing this can cause potential issues.

    I have an idea, we can use WP Cron for schedule email sending, some thing like this

    add_action('app_remove_expired', 'your_function', 10, 2);
    function your_function($appointment, $new_status)
    {
        if ($new_status == 'completed') {
            //this mean the appoinment in process, we got the end time, so we will schedule email sending at the end time
            wp_schedule_single_event(strtotime($appointment->end),'send_notification_email_for_complete_app',array($appointment));
        }
    }
    
    function send_notification_email_for_complete_app($appointment)
    {
        $name = $appointment->name;
        //your email sending code here
    }

    This is more reliable because we can set the time for sending email, also, we don't have to change the core code.

    Best regards,
    Hoang

  • Judah

    Hey @Hoang Ngo,

    I have run many many tests on this and can't get it to work.

    This is my current setup:
    1. In the appointments.php file replaced the original code. (so it marks the appointments completed at the beginning of the appointment time instead of at the end)

    2. Using the downloaded/suggested code (mu plugin that I downloaded)

    But it wasn't working. When I added in the below code, it broken my site- when I replaced the email with mine, it was still broken.

    wp_mail( your@email.com, wp_specialchars_decode( $title ), $message );

    Now what?

  • Judah

    Good news!

    It worked & I have a thought as to what was causing it to not run!

    When I was testing, I was manually creating the appointments in the system. I selected a user from the dropdown, but never filled out the fields right after the user dropdown:

    I know that when I do it the above way don't fill in the name field, the confirmation email send to the user does not contain a name.

    I am testing the above theory - I have filled in the name and an email after selecting a user and will let you know if that works. If it does - I will change the file appointments.php, line 4827 to your suggestion (to send after the appointment ends).

    Hopefully that should solve this issue - but brings up the next issue of the fields/user interaction. I already have a thread on this.

    Will update you tomorrow

  • Hoang Ngo

    Hi @Judah,

    Great news!

    I am testing the above theory - I have filled in the name and an email after selecting a user and will let you know if that works. If it does - I will change the file appointments.php, line 4827 to your suggestion (to send after the appointment ends).

    Actually, we only need to revert the changes back to original, and that's will be fine.
    One note for you, every string/text should be in quotes, or PHP will fail to run.

    Please keep me updated :slight_smile:

    Thanks a ton for your patience here Judah.

    Best regards,
    Hoang

  • Judah

    Hey @Hoang Ngo,

    It works!!!

    The issue I was having was that when I was testing, I was creating the appointments from the WP admin backend. After I selected a user from the dropdown and gave them an appointment, I incorrectly assumed that the users information (name/email) would be associated with that appointment.

    The code we were using was working the entire time, but since there was no name/email associated with the appointment it never sent the email out!

    When I manually entered in the name/email in the fields in the backend of the plugin, it sent the email out. (This is another issue for another thread but may have to do with me using the BETA version). When I create a booking as a user in the system, it works perfectly (as I make them confirm their name/email at the time of booking).

    I changed line 4827 in the appointments.php file and it began sending the emails out post appointment (the appointment status changes after the appointment ends).

    Hopefully this solution / code can be written into the new version of Appointments+ as all the information is here!

    Thanks again for your help on this issue.

    PS: Maybe for a future release there is a filter for this process. So if a user has many appointments, they don't receive a post appointment email each time!

  • Judah

    Hey @Hoang Ngo,

    I wanted to further extend this function.

    As of now, it sends an email to the user after an appointment has been marked complete. What I would like to do is the following;

    Send a different email to the Service Provider after an appointment has been marked complete.

    Below is the part of the code that states the email address to send it:

    wp_mail( $appointment->email, wp_specialchars_decode( $title ), $message );

    I am not sure what I should put in place of the $appointment->email part. Also - is there a way to incorporate it into the code? (pasted below) - I could just replicate the code and put in the new email/title/message. The best option would to just put another if statement below the current one.

    <?php
    add_action('app_remove_expired', 'appointment_completed_notifications', 10, 2);
    function appointment_completed_notifications($appointment, $new_status)
    {
    if ($new_status == 'completed') {
    $name = $appointment->name;
    $title = "How was your Appointment?";
    $message = "<p>Dear " . $name . ",</p> <p>Thanks for your appointment! We would like to hear your feedback on your recent booking.</p>
    <p>Was your service provider helpful?</p> <p>Were there any technical difficulties?</p>
    <p>Enter Feedback Here</p>";

    wp_mail( $appointment->email, wp_specialchars_decode( $title ), $message );
    }
    }

    Please let me know if/how this can be accomplished.

    Thanks,
    Judah

  • Hoang Ngo

    Hi @Judah,

    I hope you are well today.

    Send a different email to the Service Provider after an appointment has been marked complete.

    We can modify the old code, adding this behaviour after an email sent to customer. Basically, the code can be same, the only different is we need to find worker email. Here is an example how to get worker email

    function appointment_completed_notifications($appointment, $new_status)
    {
        $worker = get_userdata($appointment->worker);
        $worker_email = $worker->user_email;
        //you got the email address, now just process your code as usual for sending notification
    }

    If append to your code above, here is the full code

    add_action('app_remove_expired', 'appointment_completed_notifications', 10, 2);
    function appointment_completed_notifications($appointment, $new_status)
    {
        if ($new_status == 'completed') {
            $worker = get_userdata($appointment->worker);
            $worker_email = $worker->user_email;
            $name = $appointment->name;
            $title = "How was your Appointment?";
            $message = "<p>Dear " . $name . ",</p> <p>Thanks for your appointment! We would like to hear your feedback on your recent booking.</p>
    <p>Was your service provider helpful?</p> <p>Were there any technical difficulties?</p>
    <p>Enter Feedback Here</p>";
            wp_mail( $worker_email, wp_specialchars_decode( $title ), $message );
        }
    }

    If you have any issues please don't hesitate to let us know so we can assist

    Best regards,
    Hoang

  • Judah

    Hey @Hoang Ngo,

    Thank you for getting back to me on this one.

    I have placed the code snippet in the same mu plugin file I had originally. Now that file has two bits of code. 1 for the email for the user and the 2nd for the email for the SP.

    Also - I noticed that in the second bit of code, for the name, you put $name = $appointment->name; but used $worker->user_email;

    I swapped out the $appointment for $worker and am testing that. If thats not right (and I should keep is at $appointment for the SP) I'm sure I will find out as soon as the system sends out the email.

    $worker = get_userdata($appointment->worker);
    $worker_email = $worker->user_email;
    $name = $worker->name;
    $title = "post lesson email testing";

    Thanks again!
    Judah

  • Judah

    Hey @Hoang Ngo,

    So none of that worked - not even this code - The code below is for the SP's

    add_action('app_remove_expired', 'appointment_completed_notifications', 10, 2);
    function appointment_completed_notifications($appointment, $new_status)
    {
    if ($new_status == 'completed') {
    $worker = get_userdata($appointment->worker);
    $worker_email = $worker->user_email;
    $name = $appointment->name;
    $title = "How was your Appointment?";
    $message = "<p>Dear " . $name . ",</p> <p>Thanks for your appointment! We would like to hear your feedback on your recent booking.</p>
    <p>Was your service provider helpful?</p> <p>Were there any technical difficulties?</p>
    <p>Enter Feedback Here</p>";
    wp_mail( $worker_email, wp_specialchars_decode( $title ), $message );
    }
    }

    After I placed it in my existing functions.php file it broke my site.

    Is there any way I can place it inside of this original working function? (The code that sends an email to the user after a booking)

    //Code to send an email to the user after each lesson
    add_action('app_remove_expired', 'appointment_completed_notifications', 10, 2);
    function appointment_completed_notifications($appointment, $new_status)
    {
    if ($new_status == 'completed') {
    $name = $appointment->name;
    $title = "How was your Lesson?";
    $message ="<p>Dear " . $name . ",</p> <p>Thanks for your appointment! We would like to hear your feedback on your recent booking.</p>
    <p>Was your service provider helpful?</p> <p>Were there any technical difficulties?</p>
    <p>Enter Feedback Here</p>";

    wp_mail( $appointment->email, wp_specialchars_decode( $title ), $message );
    }

    }

    I can also create an entirely new function (like you suggested/provided) but so far it has not been working/broke the site.

    Please let me know.

    Thanks,
    Judah

  • Hoang Ngo

    Hi @Judah,

    If I understand right, this is the code you need

    add_action('app_remove_expired', 'appointment_completed_notifications', 10, 2);
    function appointment_completed_notifications($appointment, $new_status)
    {
        if ($new_status == 'completed') {
            $name = $appointment->name;
            $title = "How was your Lesson?";
            $message = "<p>Dear " . $name . ",</p> <p>Thanks for your appointment! We would like to hear your feedback on your recent booking.</p>
    <p>Was your service provider helpful?</p> <p>Were there any technical difficulties?</p>
    <p>Enter Feedback Here</p>";
            wp_mail($appointment->email, wp_specialchars_decode($title), $message);
            //send to worker, please update the text here
            $worker = get_userdata($appointment->worker);
            $worker_email = $worker->user_email;
            $name = $worker->user_login;
            $title = "post lesson email testing";
            $message = "Email to worker testing";
            wp_mail($worker_email, $title, $message);
        }
    }

    If you have any issues please don't hesitate to let us know so we can assist

    Best regards,
    Hoang

  • Judah

    Hey @Hoang Ngo,

    After changing the email address in my code, I received the user email!

    Here my current code:

    <?php

    //Code to send an email to the student after each lesson
    add_action('app_remove_expired', 'appointment_completed_notifications', 10, 2);
    function appointment_completed_notifications($appointment, $new_status)
    {
    if ($new_status == 'completed') {
    $name = $appointment->name;
    $title = "How was your Appointment?";
    $message = "<p>Dear " . $name . ",</p> <p>We would like to hear your feedback on your recent booking.</p>
    <p>Were there any technical difficulties?</p>
    <p>Enter Feedback Here</p>";

    wp_mail( 'myemail@email.com', wp_specialchars_decode( $title ), $message );

    //send to worker, please update the text here
    $worker = get_userdata($appointment->worker);
    $worker_email = $worker->user_email;
    $name = $worker->user_login;
    $title = "post lesson email testing";
    $message = "Email to worker testing";

    wp_mail($worker_email, $title, $message);

    }
    }

    Here is a screen shot of the wp debug out put.

  • Judah

    Regarding the screen shot errors -

    I corrected the first one of the unterminated comment starting line 2 - That was the old code I was working with. It was commented out but I never closed the comment - I have since deleted the mu-plugin.

    The other warnings/notices don't seem to directly reflect what we are trying to do.

    I will know in the next 5-10 minutes if the email address is the cause of the issue from the code we are working with.

    Thanks,
    Judah

  • Judah

    Hey @Hoang Ngo,

    Everything is working perfectly!

    Now to further build this out.. This may be a bit difficult/not possible but I figured I'd ask anyway. Maybe you have a suggestion:

    I would like for the SP's to be able to confirm the appointment they just gave, via the email they receive after the appointment.

    I would like for the SP to be able to click a button in their email that automatically sends an email to the admin, stating that the SP is confirming the appointment, along with the details of the appointment: name of user/date/time of the appointment.

    Is that - or something like it - possible?

    Please let me know when you get a chance.

    Thanks,
    Judah

  • Hoang Ngo

    Hi @Judah,

    I hope you are well today and I'm sorry for the delay.

    I would like for the SP's to be able to confirm the appointment they just gave, via the email they receive after the appointment.

    For now, you can let SP confirm their own Appointment, we can add a link to the email, which direct SP to their profiles page, where they can confirm the appointment. You can still add a confirm link, the idea is SP click on the link, will direct them to your site. There's will have a plugin listening for the link, and do the process, this will require extra works, and please make sure the user need to to be logged in before confirm anything.

    I would like for the SP to be able to click a button in their email that automatically sends an email to the admin, stating that the SP is confirming the appointment, along with the details of the appointment: name of user/date/time of the appointment.

    This can be done too, but like the above, require extra works. Btw, I think it will be better if we can do this automate, after any SP confirm an appointment, we can send a notification email to the admin, which same data.

    Unfortunately those features are extremely complex feature and is beyond the scope of custom coding that we could provide.

    If you need this feature now, you could try posting a job on our job board here https://premium.wpmudev.org/wordpress-development/

    If you need more informations about the plugin code, please don't hesitate to let us know so we can assist.

    Best regards,
    Hoang

  • Judah

    Hey @Hoang Ngo,

    I think we got it wrong.

    What I am trying to do is have the email be sent AFTER the appointment already happened. So the part about the confirming appointment is not actually "accepting" the appointment. Rather is it a way for the SP to let admin know that the appointment took place and if there were any issues.

    As of now, we have the email being sent to the SP after the appointment takes place. In the email there should be a button they press. When they press that button, it sends an email to the admin that contains the placeholders/macro info from that appointment.

    Is that possible?

    Please let me know,

    Thanks,
    Judah

  • Hoang Ngo

    Hi @Judah,

    I hope you are well today.

    Yes, I think we can achieve that. But in the email, we can only add a link direct to your site (not button like button in form, or ajax call), and we will have a plugin/script that listening to the link and processing your logic (sending email to admin, display notification to SP, etc).

    The link should contain appointment ID, so later, you can query database and getting that appointment information.

    Best regards,
    Hoang

  • Hoang Ngo

    Hi @Judah,

    I hope you are well today.

    Please check this code

    add_action('wp_loaded', 'listen_to_sp_confirm_appointment');
    function listen_to_sp_confirm_appointment()
    {
        //check does the GET param
        if (!isset($_GET['sp_confirm_app']) ) {
            return;
        }
    
        //get appointment id
        $app_id = isset($_GET['app_id']) ? $_GET['app_id'] : 0;
        if ($app_id == 0) {
            return;
        }
    
        if (!is_user_logged_in()) {
            return;
        }
    
        global $appointments;
        $app = $appointments->get_app($app_id);
        if (!is_object($app)) {
            return;
        }
        //now check does this user is service provider of this app
        if (get_current_user_id() != $app->worker) {
            return;
        }
    
        //from here you got the $app info, now for getting sp info, we can use this
        $sp = get_userdata($app->worker);
        //for getting the name, email, etc, please check this link http://codex.wordpress.org/Function_Reference/get_userdata
        //next, for getting user info, you can do the same like this
        $user = get_userdata($app->user_id);
        //do the rest of your logic here
    }
    
    function generate_sp_confirm_link($app_id)
    {
        return site_url('?sp_confirm_app=1&app_id=' . $app_id);
    }

    In the function listen_to_sp_confirm_appointment, you can get every data relate to the appointment, client & service provider. From here you can compose the email content and send to admin.

    The function generate_sp_confirm_link, this will generate the link, which SP should be click on to return to the site with needed parameters.

    If you need any information about the coding part, please let me know. I'm happy to assist you :slight_smile:

    Best regards,
    Hoang

  • Judah

    Hey @Hoang Ngo,

    Thank you for sending me that code. I am not quite sure how it functions/works but I had one question.

    Does this function run after an appointment already happened or before the appointment took place?

    The reason I ask is because I would like to include the appointment information (for the appointment that has happened in the past) in the email that gets sent out after the appointment took place.

    Below is the code I am currently using. I would like to add into this the information regarding the appointment that just happened.

    //send to worker, please update the text here
            $worker = get_userdata($appointment->worker);
            $worker_email = $worker->user_email;
            $name = $worker->user_login;
            $title = "How was the Appointment?";
            $message = "<p>Dear " . $name . ",</p> <p>We hope your appointment went well.</p><p>Did you experience any technical difficulties?</p>
    		<p>Please make sure you confirm the appointment was given below.</p>
    		<p><a href='mailto:xxx@xxxxxxx.com?subject=ApptConfirmed&message=has confirmed the lesson'>Confirm Lesson</a></p>
    		";
    
            wp_mail($worker_email, $title, $message);

    If the code you provided does this, please let me know where I should place it and how I can incorporate the email into it.

    Thanks again,
    Judah

  • Hoang Ngo

    Hi Judah,

    I hope you are well today.

    Thank you for sending me that code. I am not quite sure how it functions/works but I had one question.

    Does this function run after an appointment already happened or before the appointment took place?

    This code is for another scenario. This will help you to create a link in Service Provider email, and when Service Provider click on that link, it will send an email to admin.
    Follow to your code, to place a link, you can do something like this

    //send to worker, please update the text here
            $worker = get_userdata($appointment->worker);
            $worker_email = $worker->user_email;
            $name = $worker->user_login;
            $title = "How was the Appointment?";
            $message = "<p>Dear " . $name . ",</p> <p>We hope your appointment went well.</p><p>Did you experience any technical difficulties?</p>
    		<p>Please make sure you confirm the appointment was given below.</p>
    <p><a href='". generate_sp_confirm_link($appointment->id)."'>SP Confirm</a></p>
    		<p><a href='mailto:xxx@xxxxxxx.com?subject=ApptConfirmed&message=has confirmed the lesson'>Confirm Lesson</a></p>
    		";
    
            wp_mail($worker_email, $title, $message);

    For the email to admin content, please update the code I sent above, here's an example

    add_action('wp_loaded', 'listen_to_sp_confirm_appointment');
    function listen_to_sp_confirm_appointment()
    {
        //check does the GET param
        if (!isset($_GET['sp_confirm_app'])) {
            return;
        }
    
        //get appointment id
        $app_id = isset($_GET['app_id']) ? $_GET['app_id'] : 0;
        if ($app_id == 0) {
            return;
        }
    
        if (!is_user_logged_in()) {
            return;
        }
    
        global $appointments;
        $app = $appointments->get_app($app_id);
        if (!is_object($app)) {
            return;
        }
        //now check does this user is service provider of this app
        if (get_current_user_id() != $app->worker) {
            return;
        }
    
        //from here you got the $app info, now for getting sp info, we can use this
        $sp = get_userdata($app->worker);
        //for getting the name, email, etc, please check this link http://codex.wordpress.org/Function_Reference/get_userdata
        //next, for getting user info, you can do the same like this
        $user = get_userdata($app->user_id);
        //do the rest of your logic here
        $subject = 'Your text here';
        $content = 'YOur text here';
        wp_mail('admin_email', $subject, $content);
    }

    If you have any issues please don't hesitate to let us know so we can assist

    Best regards,
    Hoang

  • Judah

    Hey @Hoang Ngo,

    How are you?

    I have an issue with the original mu-plugin that we wrote. So it turns out that when an appointment is canceled, the post-appointment email is still being sent.

    Here is the code I am using:

    <?php
    //Code to send an email to the student after each lesson
    add_action('app_remove_expired', 'appointment_completed_notifications', 10, 2);
    function appointment_completed_notifications($appointment, $new_status)
    {
    if ($new_status == 'completed') {
    $name = $appointment->name;
    $title = "How was your Appointment?";
    $message = "<p>Dear " . $name . ",</p> <p>We would like to hear your feedback on your recent booking.</p>
    <p>Were there any technical difficulties?</p>
    <p>Enter Feedback Here</p>";
    wp_mail( 'myemail@email.com', wp_specialchars_decode( $title ), $message );
    //send to worker, please update the text here
    $worker = get_userdata($appointment->worker);
    $worker_email = $worker->user_email;
    $name = $worker->user_login;
    $title = "post lesson email testing";
    $message = "Email to worker testing";
    wp_mail($worker_email, $title, $message);
    }
    }

    Is there any way to fix this so that the email gets sent out only when a confirmed appointment turns into a completed appointment?

    Please let me know,

    Thanks,
    Judah

  • Hoang Ngo

    Hi @Judah,

    I'm good, thanks :slight_smile:. I hope you are well too.

    The code still look good, I've check the function remove_appointments in the file /appointments/appointments.php. From there, you can see that only appointments with status confirmed or paid can turn into completed

    Perhaps this is another case, or I'm missing something? Can you please provide me more information on how you get this issue?

    Thanks!

    Best regards,
    Hoang

    • Judah

      Hey @Hoang Ngo,

      For some reason the messages have stopped being sent out.

      I am still using the same version of A+ and have made no updates. (Wordpress was automatically updated though).

      I am not able to update A+ as there are too many bugs with the new version and my manual fixes are no longer working.

      Do you know why it would just stop working?

      Any debugging help is appreciated.

      Thanks,
      Judah

  • Hoang Ngo

    Hi @Judah,

    I don't think we can do that. But there still a way, as you know $appointment->user is the user_id of client, and $appointment->worker is the user_id of the Service Provider.

    So you can get the user data by this function
    https://codex.wordpress.org/Function_Reference/get_userdata
    So example, you can do this

    $userdata = get_userdata($appointment->user);
    $name = $userdata->display_name;

    If you have any additional issues, please let us know and we'll be happy to help.

    Best regards,
    Hoang

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.