php – 在Woocommerce中取消订单时向客户发送电子邮件

我正在尝试在订单取消时向客户发送电子邮件.认情况下,woocommerce仅将此电子邮件仅发送给网站的管理员.
代码解决了网络上相关帖子的问题:

function wc_cancelled_order_add_customer_email( $recipient, $order ){
   return $recipient . ',' . $order->billing_email;
}
add_filter( 'woocommerce_email_recipient_cancelled_order', 'wc_cancelled_order_add_customer_email', 10, 2 );
add_filter( 'woocommerce_email_recipient_Failed_order', 'wc_cancelled_order_add_customer_email', 10, 2 );

然而,似乎woocommerce完全删除了那些过滤器钩子.
有没有办法做到这一点?

提前致谢!

解决方法:

在这个隐藏在woocommerce_order_status_changed动作钩子中的自定义函数中,我定位“取消”和“失败”的订单向客户发送相应的电子邮件通知(管理员将通过WooCommerce自动通知接收它):

add_action('woocommerce_order_status_changed', 'send_custom_email_notifications', 10, 4 );
function send_custom_email_notifications( $order_id, $old_status, $new_status, $order ){
    if ( $new_status == 'cancelled' || $new_status == 'Failed' ){
        $wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances
        $customer_email = $order->get_billing_email(); // The customer email
    }

    if ( $new_status == 'cancelled' ) {
        // change the recipient of this instance
        $wc_emails['WC_Email_Cancelled_Order']->recipient = $customer_email;
        // Sending the email from this instance
        $wc_emails['WC_Email_Cancelled_Order']->trigger( $order_id );
    } 
    elseif ( $new_status == 'Failed' ) {
        // change the recipient of this instance
        $wc_emails['WC_Email_Failed_Order']->recipient = $customer_email;
        // Sending the email from this instance
        $wc_emails['WC_Email_Failed_Order']->trigger( $order_id );
    } 
}

代码放在活动子主题(或主题)的function.PHP文件中,或者放在任何插件文件中.

这应该适用于WooCommerce 3

If you need, instead of changing the email, you can add it, to existing recipients:

06001

相关回答:Send an email notification when order status change from pending to cancelled

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...