当订单状态从待处理变成取消时发送电子邮件通知

4
在以前版本的Woocommerce中,当订单从待处理状态变为取消状态时(在我的情况下,这是在管理员库存部分设置的指定时间之后发生),会自动发送电子邮件通知。
在WooCommerce 3.0.8中,他们删除了此自动化并标记为修复: https://github.com/woocommerce/woocommerce/blob/master/CHANGELOG.txt 而拉取请求在这里: https://github.com/woocommerce/woocommerce/pull/15170/files 我想恢复此功能,但显然将此行复制/粘贴回Woocommerce核心文件不是一个好主意,因为它会在平台更新时被覆盖。
我知道最好的方法是创建一个函数并通过functions.php钩入取消订单操作,但看了一下后,我有点迷失了如何做到这一点。这是被替换的那一行:
add_action( 'woocommerce_order_status_pending_to_cancelled_notification', array( $this, 'trigger' ), 10, 2 );

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

11

好消息:使用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+上仍然有效)


1
它与WooCommerce v4.8.0兼容,至今仍在正常工作。谢谢! - noobnicks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接