WooCommerce中所有现有处理订单的自动完成状态 [英] Auto completed status for all existing processing orders in WooCommerce
问题描述
我正在WooCommerce上使用以下代码的小和平度: 从此答案 到自动完成付款处理订单:
I am using on WooCommerce this little peace of code from this answer to autocomplete paid processing orders:
/**
* 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"挂钩上处于保留状态.
But the problem is that I use a special payment gateway by SMS which API is bridged on 'cod' payment method, and the orders stay sometimes in on-hold status on this 'woocommerce_thankyou' hook.
因此,我将需要一直扫描处理中"的订单,以将其传递给完整状态.我尝试了不同的方法和操作,但是我无法按预期工作.
So I will need to scan all the time the 'processing' orders to pass them in complete status. I have tried different things and hooks, but I cant get it work as expected.
我该怎么做?
谢谢
推荐答案
要使此功能正常运行,您只需要一个小功能即可对所有订单进行处理"扫描.状态在初始"钩子上显示,并将此状态更新为已完成".
To get this working you just need a little function that will scan all orders with a "processing" status on the 'init' hook, and that will update this status to "completed".
这是代码:
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' );
此代码已经过测试并且可以正常工作.
This code is tested and works.
代码进入您的活动子主题(或主题)的function.php文件中.或在任何插件php文件中.
广告&更新
发送两次的电子邮件通知周围有一个小错误,在此处解决:
避免对某些自动完成的订单重复发送电子邮件通知
There is a little bug around email notifications sent twice that is solved in here:
Avoid repetitive emails notification on some auto completed orders
这篇关于WooCommerce中所有现有处理订单的自动完成状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!