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 桥接在cod"付款方式上,并且订单有时会在此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.

我该怎么做?

谢谢

推荐答案

要使此工作正常运行,您只需要一个小功能即可扫描所有带有处理"命令的订单.'init' 钩子上的状态,并且会将此状态更新为已完成".

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 文件中.

建议和更新

电子邮件通知发送两次有一个小错误,在这里解决:
避免在某些自动完成的订单上重复发送电子邮件通知

相关文章