WooCommerce 向管理员发送电子邮件通知以获取特定订单状态 [英] WooCommerce send email notification to admin for specific order status
问题描述
以下是我在订单状态变为处理中"时向管理员发送通知电子邮件的代码:
Here below is my code for sending notification email to admin when order status becomes "processing":
add_action( 'woocommerce_checkout_order_processed', 'process_new_order_notification', 20, 1 );
function process_new_order_notification( $order_id ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
//Order status processing
if( ! $order->has_status( 'processing' ) ) return;
// Send "New Email" notification (to admin)
WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}
但它不起作用,因为当订单状态变为处理时,管理员不会收到任何电子邮件.我认为我的代码有问题.有什么帮助吗?
But it doesn't work as the admin don't receive any email when order status becomes processing. I think there something wrong with my code. Any help?
推荐答案
您可以使用 woocommerce_order_status_$STATUS_TRANSITION[to]
复合钩子进行处理"状态转换改变 $STATUS_TRANSITION[to]
到 processing
,这将简化和压缩代码,例如:
You can use woocommerce_order_status_$STATUS_TRANSITION[to]
composite hook for "processing"status transition changing $STATUS_TRANSITION[to]
to processing
, which will simplify and compact the code, like:
add_action( 'woocommerce_order_status_processing', 'process_new_order_notification', 20, 2 );
function process_new_order_notification( $order_id, $order ) {
// Send "New Email" notification (to admin)
WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}
代码位于活动子主题(或活动主题)的 function.php 文件中.它应该会更好地工作.
Code goes in function.php file of your active child theme (or active theme). It should better work.
自从 WooCommerce 5+: 允许在 WooCommerce 5+ 中重新发送新订单通知
这篇关于WooCommerce 向管理员发送电子邮件通知以获取特定订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!