重新发送WooCommerce客户挂起订单电子邮件通知的程序化方法

5
我注意到客户等待订单电子邮件不可用,因此我尝试使用单个操作替换操作,以发送适当的电子邮件。这似乎行得通,除了等待状态。我看不出除了在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' );

具体是什么问题?你的代码中有什么问题或者不起作用的地方吗? - random_user_name
如果订单处于处理中或已完成状态,则代码可以正常工作,但如果订单处于挂起状态,则不会发送电子邮件。在调试日志中没有出现任何消息,看起来似乎 customer_on_hold_order 不在 $mails 中,但原始电子邮件已正确发送。 - Bush Al
1
抱歉,Loic,我一定是误按了那个 - 现在重新勾选了。一切都好。 - Bush Al
1个回答

7
我已经重新审查和精简了您的代码,因为其中存在一些错误,例如在if ($mail->id == eml){中使用eml作为变量名时发生了拼写错误...此外,要从WC_Order对象中获取订单编号,您应该使用$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中。

这段代码已经在WooCommerce 3+中进行了测试,并且现在可以正常工作,用于处理待处理订单状态的电子邮件通知重新发送。


1
它之前运行得非常完美,但在更新到WooCommerce 3.5.1之后,只有新订单邮件没有被发送。有任何想法吗?有任何已知的错误(我在Woo Git上没有找到)? - Pankaj Verma
哇,这真的为我节省了很多工作,因为我要测试的服务涉及到十几封电子邮件!谢谢! - mpemburn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接