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 ?
CodePudding user response:
It's because there are several bugs in your code!
You're using
WP travel plugin
. This plugin register its custom post type as"itinerary-booking"
, but in your code, you're checking for a custom post type called"Booking"
which doesn't exist, so that condition never returns true!You could navigate to this path:
"your website folder > wp-content > plugins > wp-travel > inc"
and open upclass-post-types.php
file on line126
you see thisregister_post_type( 'itinerary-booking', $args );
Also, you're using
transition_post_status
action hook which is used for the checking a post status. The status ofpending
andbooked
are custom post meta data and saved into the database under thewp_travel_booking_status
name and have nothing to do with the post status itself! Which means it's totally different than what you're trying to do.
So, this filter hook transition_post_status
is not responsible to pick up those statuses.
What can you do about it?
- Well, you could change your hook to
save_post
action hook which means every time that you update your post, it'll fire! - You could change your conditional check to look for
itinerary-booking
custom post type. - 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:
- I used
die
statement just to give you an example, so make sure to replacedie("Send your email here!");
with your custom function that sends out emails! - Status of
booked
is lowercase, so don't change it to uppercase like the one you tried in your code! Otherwise it won't work! - When your post status is not "booked", you don't have to do anything, i used
die("your post status is not booked!");
just to give you an example!
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 a confirmation email 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:
- This will make sure that you only send your confirmation email once!
- If the confirmation email data is empty in the database, it'll send the email and update the value in the database and won't send extra emails.
- Also, I've stored a meta data in your database called
custom_confirmation_email
and used'sent-at-'. date('M_d_Y-H_i_s')
as its value which look like this:sent-at-Oct_01_2021-17_42_42
.
All of the snippets and explanations have been fully tested and work!