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

Sending email to customer on cancelled order in 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 操作挂钩中的自定义函数中,我针对 "cancelled" 和 "failed" 订单发送相应的电子邮件通知给客户(因为管理员将通过 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:

// Add a recipient in this instance
$wc_emails['WC_Email_Failed_Order']->recipient .= ',' . $customer_email;

相关回答: