phpwordpresspluginswordpress-themingwordpress-admin

Wordpress how to send email confirmations on post status change


I have a custom post type called "Booking" in WP travel plugin. I want it to fire an event when i change the status of that custom post type from 'pending' to 'booked'.

The event should send a confirmation email to the user in question.

I tried the following :

function show_alert_msg( $new_status, $old_status, $post ) {
    if ( 'Booked' === $new_status && 'pending' !== $old_status && $post->post_type=='Booking' )
        
     {
         echo '<script>alert(Show message after transition to booked)</script>' ;
    }else{
        echo '<script>alert(failed)</script>' ;

    }
}
add_action( 'transition_post_status', 'show_alert_msg', 10, 3 );

but whenever i change the status nothing happens.

what i am doing wrong? and how can I approach this correctly so that when the booking status changes from pending to Booked, the confirmation email is sent to the user ?


Solution

  • It's because there are several bugs in your code!

    enter image description here enter image description here

    So, this filter hook transition_post_status is not responsible to pick up those statuses.


    Now what can you do about it?

    1. Well, you could change your hook to save_post action hook which means every time that you update your post, it'll fire!
    2. You could change your conditional check to look for itinerary-booking custom post type.
    3. Also you could use get_post_meta function, to check for your status change!

    Now putting it all together, you could use the following code:

    add_action( 'save_post', 'sending_email_confirmation_to_user');
    
    function sending_email_confirmation_to_user( $post_id )
    {
    
      if ( 'itinerary-booking' == get_post_type( $post_id ) ){
    
        $post_status_after_update = get_post_meta( $post_id, 'wp_travel_booking_status', true );
    
        if( 'booked' === $post_status_after_update ){
          die("Send your email here!");
        } else {
          die("your post status is not booked!");
        }
      }
    
    }
    

    Note:


    Update

    When you use save_post hook, it runs every time you update your custom post data, BUT you do NOT want to send an email every single time. Right?

    When you change the status from 'pending' to 'booked' you could save a meta data in the database so that you know you've already sent an email confirmation and you don't want to send another email. You could do that using update_post_meta function.

    add_action( 'save_post', 'sending_email_confirmation_to_user');
    
    function sending_email_confirmation_to_user( $post_id )
    {
    
      if ( 'itinerary-booking' == get_post_type( $post_id ) ){
    
        $post_email_status = get_post_meta( $post_id, 'custom_confirmation_email', true );
        $post_status_after_update = get_post_meta( $post_id, 'wp_travel_booking_status', true );
    
        if( 'booked' === $post_status_after_update && empty( $post_email_status )){
          $email_sent = update_post_meta( $post_id, 'custom_confirmation_email', 'sent-at-'. date( 'M_d_Y-H_i_s' ) );
          die("Send your email here!");
        } 
      }
    
    }
    

    Note:


    All of the snippets and explanations have been fully tested and work!