当订单状态从待处理更改为已取消时发送电子邮件通知

Send an email notification when order status change from pending to cancelled

在以前版本的 Woocommerce 中,当订单从待定状态更改为取消状态时,会自动发送电子邮件通知(在我的例子中,这发生在管理员的库存部分中设置的分配时间之后)。

在 WooCommerce 3.0.8 中,他们删除了这个自动化并标记为修复: https://github.com/woocommerce/woocommerce/blob/master/CHANGELOG.txt

拉取请求在这里: https://github.com/woocommerce/woocommerce/pull/15170/files

我正在寻求恢复此功能,但显然 copy/pasting 返回 Woocommerce 核心文件的这一行不是一个好主意,因为它会在平台更新时被覆盖。

我知道最好的方法是创建一个函数并通过 functions.php 连接到已取消的订单操作,但看了之后我对如何执行此操作有点迷茫。这是被替换的行:

add_action( 'woocommerce_order_status_pending_to_cancelled_notification', array( $this, 'trigger' ), 10, 2 );

如何恢复这个旧的自动化功能?

好消息: 使用带有自定义函数挂钩的 woocommerce_order_status_pending_to_cancelled 动作挂钩,彻底解决您的问题:

add_action('woocommerce_order_status_pending_to_cancelled', 'cancelled_send_an_email_notification', 10, 2 );
function cancelled_send_an_email_notification( $order_id, $order ){
    // Getting all WC_emails objects
    $email_notifications = WC()->mailer()->get_emails();

    // Sending the email
    $email_notifications['WC_Email_Cancelled_Order']->trigger( $order_id );
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

经过测试并在 WooCommerce 3+ 中完美运行 (仍适用于 4.8+ 版本)