在 Woocommerce 中就取消的订单向客户发送电子邮件 [英] Sending email to customer on cancelled order in Woocommerce

查看:38
本文介绍了在 Woocommerce 中就取消的订单向客户发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to send an email to the client when an order gets cancelled. By default, woocommerce only sends this email only to the admin of the site. This code has solved the issue for related posts on the web:

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 似乎完全删除了这些过滤器挂钩.有没有办法做到这一点?

However, it seems like woocommerce removed those filter hooks completely. Is there any way of doing this?

提前致谢!

推荐答案

在这个挂在 woocommerce_order_status_changed 动作钩子中的自定义函数中,我的目标是取消"和失败"" 订单向客户发送相应的电子邮件通知(因为管理员将通过 WooCommerce 自动通知在他身边收到它):

In this custom function hooked in woocommerce_order_status_changed action hook, I am targeting "cancelled" and "failed" orders sending an the corresponding email notification to the customer (as admin will receive it on his side by WooCommerce automated notifications):

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+

This should works in 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;

<小时>

相关答案:当订单状态从待处理变为取消时发送电子邮件通知

这篇关于在 Woocommerce 中就取消的订单向客户发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆