【问题标题】:Resend programmatically a WooCommerce customer_on_hold_order email notification以编程方式重新发送 WooCommerce customer_on_hold_order 电子邮件通知
【发布时间】:2017-08-28 23:50:39
【问题描述】:

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

这似乎工作除了暂停状态。除了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' );

【问题讨论】:

  • 具体是什么问题?您的代码中的哪些内容/不起作用?
  • 如果订单处于处理或完成状态,代码工作正常,但如果处于暂停状态,则不会发送电子邮件。调试日志中没有出现任何消​​息,看起来好像 customer_on_hold_order 不在 $mails 中,但原始电子邮件已正确发送
  • 对不起 Loic,我一定是按错了——现在重新勾选。一切都好

标签: php wordpress woocommerce orders email-notifications


【解决方案1】:

我已经重新访问并压缩了您的代码,因为其中存在一些错误,例如 if ( $mail->id == eml ){ 中的拼写错误,用于 eml 作为变量名......另外从 获取订单 ID 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+ 中经过测试,现在可以在重新发送时正常处理暂停订单状态电子邮件通知

【讨论】:

  • 它工作得非常完美,但是在更新到 woocommerce 3.5.1 之后,只有新的订单电子邮件没有被触发。任何的想法?任何已知的错误(我在 woo git 上没有找到)?
  • 哇,在测试影响十多封电子邮件的服务时,这确实为我节省了 很多 的工作!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-07
  • 2018-09-03
相关资源
最近更新 更多