避免在某些自动完成的订单上重复发送电子邮件通知 [英] Avoid repetitive emails notification on some auto completed orders
问题描述
我正在WooCommerce 从此答案 的自动编码中自动使用此代码. -基于支付网关的完整付款处理订单:
I'm using this little peace of code on WooCommerce from this answer to auto-complete paid processing orders based on payment gateways:
/**
* AUTO COMPLETE PAID ORDERS IN WOOCOMMERCE
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cod' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cheque' ) ) {
return;
}
// "completed" updated status for paid Orders with all others payment methods
else {
$order->update_status( 'completed' );
}
}
这几乎可以完美地工作
主要使用特殊的 SMS支付网关,该API桥接在鳕鱼"付款方式上,并且可以在前端外部"woocommerce_thankyou"之后处理付款.在这种情况下,处于保留状态的订单会随后传递到正在处理状态.为了在这些情况下自动执行自动完成行为,我使用了 此答案 中的其他代码它的工作原理:
Mainly using a special payment gateway by SMS which API is bridged on 'cod' payment method and that can process payment after 'woocommerce_thankyou, out side frontend. In that case the ON HOLD status orders are passed afterward to PROCESSING status. To automate an autocomplete behavior on those cases, I use this other peace of code from this answer and it works:
function auto_update_orders_status_from_processing_to_completed(){
// Get all current "processing" customer orders
$processing_orders = wc_get_orders( $args = array(
'numberposts' => -1,
'post_status' => 'wc-processing',
) );
if(!empty($processing_orders))
foreach($processing_orders as $order)
$order->update_status( 'completed' );
}
add_action( 'init', 'auto_update_orders_status_from_processing_to_completed' );
问题:我收到有关新完成订单的重复电子邮件通知.
THE PROBLEM: I am getting repetitive emails notifications concerning the new completed orders.
如何避免这种重复的电子邮件通知情况?
How can I avoid this repetitive email notifications cases?
谢谢
推荐答案
已更新 (2019)
为Woocommerce 3+添加了版本代码-添加了Woocommerce版本兼容性.
为避免这种重复邮件通知的奇怪现象,当使用WordPress get_post_meta()
函数用于每个已处理的订单.
To avoid this strange fact of repetitive email notifications, is possible to create a custom meta key/value for each processed order, when changing order status to completed, using WordPress update_post_meta()
function. Then we will test before in a condition, if this custom meta data key/value exist with get_post_meta()
function for each processed order.
所以您的两个代码段现在是:
So your two code snippets will be now:
1)在WOOCOMMERCE中自动完成已付款的订单 (2019年更新)
对于woocommerce 3 +:
For woocommerce 3+:
add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
if ( ! $order->has_status('completed') && $order->get_meta('_order_processed') != 'yes') {
$order->update_meta_data('_order_processed', 'yes');
$status = 'completed';
}
return $status;
}
对于所有woocommerce版本(自2.5+版本以来的兼容性):
For all woocommerce versions (compatibility since version 2.5+):
add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order = null ) {
// Getting the custom meta value regarding this autocomplete status process
$order_processed = get_post_meta( $order_id, '_order_processed', true );
// Getting the WC_Order object from the order ID
$order = wc_get_order( $order_id );
if ( ! $order->has_status( 'completed' ) && $order_processed != 'yes' ) {
$order = wc_get_order( $order_id );
// setting the custom meta data value to yes (order updated)
update_post_meta($order_id, '_order_processed', 'yes');
$order->update_status( 'completed' ); // Update order status to
}
return $status;
}
2)扫描所有处理中"的订单 以自动完成订单(增加了Woocommerce兼容性)
add_action( 'init', 'auto_update_orders_status_from_processing_to_completed' );
function auto_update_orders_status_from_processing_to_completed(){
if( version_compare( WC_VERSION, '3.0', '<' ) {
$args = array('numberposts' => -1, 'post_status' => 'wc-processing'); // Before WooCommerce version 3
} else {
$args = array('limit' => -1, 'status' => 'processing'); // For WooCommerce 3 and above
}
// Get all current "processing" customer orders
$processing_orders = (array) wc_get_orders( $args );
if( sizeof($processing_orders) > 0 ){
foreach($processing_orders as $order ) {
// Woocommerce compatibility
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
// Checking if this custom field value is set in the order meta data
$order_processed = get_post_meta( $order_id, '_order_processed', true );
if (! $order->has_status( 'completed' ) && $order_processed != 'yes' ) {
// Setting (updating) custom meta value in the order metadata to avoid repetitions
update_post_meta( $order_id, '_order_processed', 'yes' );
$order->update_status( 'completed' ); // Updating order status
}
}
}
}
代码进入您的活动子主题(或主题)的function.php文件中.或在任何插件php文件中.
我已经测试过此代码,并且该代码应对您有效(由于您的特定的短信桥接付款方式)
I have test this code and it should work for you (due to your particular SMS bridged payment method)
这篇关于避免在某些自动完成的订单上重复发送电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!