WooCommerce 中所有现有处理订单的自动完成状态

Auto completed status for all existing processing orders in WooCommerce

我在 WooCommerce 上使用这段代码 来自动完成付费处理订单:

/**
 * 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' );
    }
} 

但问题是我使用了一个特殊的短信支付网关,API 桥接到 'cod' 支付方式,订单有时会在 [=23] 上处于暂停状态=] 勾.

所以我将需要一直扫描 'processing' 订单以完整传递它们。我尝试了不同的东西和钩子,但我无法让它按预期工作。

我该怎么做?

谢谢

要实现此功能,您只需要一个小功能即可扫描 'init' 挂钩上状态为“正在处理”的所有订单,并将此状态更新为“已完成”。

代码如下:

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' );

此代码已经过测试并且有效。

代码进入您的活动子主题(或主题)的 function.php 文件。或者在任何插件 php 文件中。

ADVICE & UPDATE

There is a little bug around email notifications sent twice that is solved in here:

WooCommerce 虚拟订单可以在付款后自动标记为“已完成”,只需将一点代码添加到自定义插件或您的主题 functions.php 文件中。默认情况下,WooCommerce 会在付款成功后将虚拟可下载订单标记为“已完成”,这是有道理的,但一些店主甚至希望能够在付款后自动将虚拟订单标记为完成,例如在网站的情况下在不需要采取进一步行动的情况下接受捐款。为此,请使用以下代码,该代码基于核心虚拟可下载的已完成订单状态:

add_filter( 'woocommerce_payment_complete_order_status', 'virtual_order_payment_complete_order_status', 10, 2 );

function virtual_order_payment_complete_order_status( $order_status, $order_id ) {
  $order = new WC_Order( $order_id );

  if ( 'processing' == $order_status &&
       ( 'on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status ) ) {

    $virtual_order = null;

    if ( count( $order->get_items() ) > 0 ) {

      foreach( $order->get_items() as $item ) {

        if ( 'line_item' == $item['type'] ) {

          $_product = $order->get_product_from_item( $item );

          if ( ! $_product->is_virtual() ) {
            // once we've found one non-virtual product we know we're done, break out of the loop
            $virtual_order = false;
            break;
          } else {
            $virtual_order = true;
          }
        }
      }
    }

    // virtual order, mark as completed
    if ( $virtual_order ) {
      return 'completed';
    }
  }

  // non-virtual order, return original status
  return $order_status;
}

您也可以使用插件自动完成订单

这是插件 URL : https://wordpress.org/plugins/woocommerce-autocomplete-order/screenshots/

请告诉我哪个对你有用。

谢谢。