[英]Custom PHP snipper for email notification to the admin for pending order status in WooCommerce causing duplicated emails for processed orders
One of the payment methods we use on the site sets new orders to "Pending" mode which by default have no email notifications.我们在网站上使用的一种付款方式将新订单设置为“待处理”模式,默认情况下没有电子邮件通知。 I've found the following snippet and added it to my child's theme functions.php file.
我找到了以下代码片段并将其添加到我孩子的主题 functions.php 文件中。
It does send a single email notifications for pending orders correctly, but it also causes double emails to be sent for other orders using other payment methods even though their status is "processing".它确实为挂单正确发送了一封电子邮件通知,但它也会导致使用其他付款方式的其他订单发送双封电子邮件,即使它们的状态为“正在处理”。 How would you suggest to fix it?
你会建议如何修复它? Thank you for your time & help.
感谢您的时间和帮助。
// New order notification for Pending Orders
add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );
function pending_new_order_notification( $order_id ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Only for "pending" order status
if( ! $order->has_status( 'pending' ) ) return;
// Send "New Email" notification (to admin)
WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}
The problem is your use of the hook woocommerce_checkout_order_processed
.问题是您使用了钩子
woocommerce_checkout_order_processed
。 Instead, try woocommerce_order_status_pending
.相反,请尝试
woocommerce_order_status_pending
。 I believe the rest of your code should be fine.我相信您的其余代码应该没问题。 Basic use of this as well as some other useful hooks can be found here .
可以在这里找到它的基本用法以及其他一些有用的钩子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.