以编程方式重新发送 WooCommerce customer_on_hold_order 电子邮件通知

Resend programmatically a WooCommerce customer_on_hold_order email notification

我注意到客户保留的订单电子邮件不可用,因此我尝试将这些操作替换为可以发送适当电子邮件的单个操作。

除了暂停状态外,这似乎有效。除了不在 class-wc-meta-box-order-actions.php 中的 $available_emails 之外,我看不出暂停和处理案例之间有什么区别,我已经删除了所有其他案例,它们仍然有效。

我做错了什么?有没有办法让这成为可能?

我的代码是:

    function ulmh_resend1( $actions ) {
    $actions['ulmh_resend'] = __( 'Resend Email', 'text_domain' );
    return $actions;
}
function ulmh_resend2( $order ) {
    $mailer = WC()->mailer();
    $mails = $mailer->get_emails();
    if ($order->has_status( 'on-hold' )) {
    $eml = 'customer_on_hold_order';    
    }elseif ($order->has_status( 'processing' )) {
    $eml = 'customer_processing_order'; 
    }elseif ($order->has_status( 'completed' )) {
    $eml = 'customer_completed_order';  
    } else {
    $eml = "nothing";
    }   
    if ( ! empty( $mails ) ) {
        foreach ( $mails as $mail ) {
            if ( $mail->id == eml ) {
               $mail->trigger( $order->id );
            }
         }
    }
}
function ulmh_resend3( $order_emails ) {
    $remove = array( 'new_order', 'cancelled_order', 'customer_processing_order', 'customer_completed_order', 'customer_invoice' );
    $order_emails = array_diff( $order_emails, $remove );
    return $order_emails;
}   
add_action( 'woocommerce_order_actions', 'ulmh_resend1' );
add_action( 'woocommerce_order_action_ulmh_resend', 'ulmh_resend2' );
add_filter( 'woocommerce_resend_order_emails_available', 'ulmh_resend3' );

我已经重新访问并压缩了你的代码,因为那里有一些错误,比如 if ( $mail->id == eml ){ 中的拼写错误 for eml 作为变量名......还有从 WC_Order 对象获取订单 ID 你应该使用 $order->get_id() 方法而不是 $order->id.

这是这个新的功能代码:

add_action( 'woocommerce_order_actions', 'ulmh_resend1' );
function ulmh_resend1( $actions ) {
    $actions['ulmh_resend'] = __( 'Resend Email', 'text_domain' );
    return $actions;
}

add_action( 'woocommerce_order_action_ulmh_resend', 'ulmh_resend2' );
function ulmh_resend2( $order ) {
    $wc_emails = WC()->mailer()->get_emails();
    if( empty( $wc_emails ) ) return;

    if ($order->has_status( 'on-hold' ))
        $email_id = 'customer_on_hold_order';
    elseif ($order->has_status( 'processing' ))
        $email_id = 'customer_processing_order';
    elseif ($order->has_status( 'completed' ))
        $email_id = 'customer_completed_order';
    else
        $email_id = "nothing";

    foreach ( $wc_emails as $wc_mail )
        if ( $wc_mail->id == $email_id )
            $wc_mail->trigger( $order->get_id() );
}

add_filter( 'woocommerce_resend_order_emails_available', 'ulmh_resend3' );
function ulmh_resend3( $order_emails ) {
    $remove = array( 'new_order', 'cancelled_order', 'customer_processing_order', 'customer_completed_order', 'customer_invoice' );
    $order_emails = array_diff( $order_emails, $remove );
    return $order_emails;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

This code is tested in WooCommerce 3+ and works fine now for on-hold order status email notifications, when resending