Wordpress 如何在 post 状态更改时发送电子邮件确认

Wordpress how to send email confirmations on post status change

我在 WP travel 插件中有一个名为“预订”的自定义 post 类型。当我将该自定义 post 类型的状态从 'pending' 更改为 'booked'.

时,我希望它触发一个事件

活动应向相关用户发送一封确认电子邮件。

我尝试了以下方法:

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 );

但是每当我改变状态时什么都没有发生。

我做错了什么?以及如何正确处理此问题,以便当预订状态从待处理更改为已预订时,确认电子邮件会发送给用户?

因为你的代码有几个bug!

  • 您正在使用 WP travel plugin。此插件将其自定义 post 类型注册为 "itinerary-booking",但在您的代码中,您正在检查名为 "Booking" 的自定义 post 类型,该类型不存在,因此条件从不 returns 真!

    You could navigate to this path:
    "your website folder > wp-content > plugins > wp-travel > inc"
    and open up class-post-types.php file on line 126 you see this register_post_type( 'itinerary-booking', $args );

  • 此外,您正在使用 transition_post_status 操作挂钩,用于检查 post 状态。 pendingbooked 的状态是自定义 post 元数据,并以 wp_travel_booking_status 名称保存到数据库中,与 post 状态本身无关!这意味着它与您尝试做的完全不同。

因此,此过滤器挂钩 transition_post_status 不负责获取这些状态。

  • 此外,当尝试 debug/test 你的代码时,特别是在 transition_post_statussave_post 钩子上,做 NOT使用 javascript alert, console.log and/or php echo, print_r, var_dump 等因为当你打那些挂钩、打印、回显、警报将不起作用。只需使用 php die 功能即可停止应用程序并让您知道您中招了。

  • 大小写也很重要!确保您为您的值使用正确的大小写,例如,在这种情况下,一切都是小写的。所以请注意这一点!


现在你能做些什么呢?

  1. 好吧,您可以将挂钩更改为 save_post 动作挂钩,这意味着每次您更新 post 时,它都会触发!
  2. 您可以更改条件检查以查找 itinerary-booking 自定义 post 类型。
  3. 您也可以使用 get_post_meta 函数来检查您的状态变化!

现在将它们放在一起,您可以使用以下代码:

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!");
    }
  }

}

注:

  • 我使用 die 语句只是为了给您举个例子,所以请务必将 die("Send your email here!"); 替换为您发送电子邮件的自定义函数!
  • booked 的状态是小写,所以不要像您在代码中尝试的那样将其更改为大写!不然不行!
  • 当你的post状态不是“预订”时,你不需要做任何事情,我用die("your post status is not booked!");只是为了给你举个例子!

更新

当您使用 save_post 挂钩时,它会在您每次更新自定义 post 数据时运行,但是 您不想每次都发送电子邮件单次。对吧?

当您将状态从 'pending' 更改为 'booked' 时,您可以在数据库中 save a meta data 这样您就知道您已经发送了一封电子邮件确认并且您不想发送另一封电子邮件。您可以使用 update_post_meta 函数来做到这一点。

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!");
    } 
  }

}

注:

  • 这将确保您只发送一次电子邮件确认!
  • 如果数据库中的电子邮件确认数据为空,它将发送电子邮件并更新数据库中的值,不会发送额外的电子邮件。
  • 此外,我在您的数据库中存储了一个名为 custom_confirmation_email 的元数据,并使用 'sent-at-'. date('M_d_Y-H_i_s') 作为其值,如下所示: sent-at-Oct_01_2021-17_42_42 这是日期和时间状态第一次从'pending'变为'booked'。

所有片段和解释都已经过全面测试并且有效!