如何在WooCommerce中付款后查看订单状态

Bor*_*nka 9 php wordpress payment-gateway orders woocommerce

我需要在收到付款后自动更改已完成的订单状态,但前提是订单状态为"正在处理".我找到了片段,在每种情况下都会使订单状态完成,但是在成功付款更改后我的付款插件会返回数据并更改"处理"的订单状态.我想在成功后将其更改为"已完成",如果状态不是"处理",则不要更改它.我遇到的主要问题是我不知道如何获得收到的状态订单.

这是我的代码:

add_filter( 'woocommerce_thankyou', 'update_order_status', 10, 2 );

function update_order_status( $order_id ) {
   $order = new WC_Order( $order_id );
   $order_status = $order->get_status();    
   if ('processing' == $order_status) {    
       $order->update_status( 'completed' );    
    }    
 //return $order_status;
}
Run Code Online (Sandbox Code Playgroud)

我已经弄清楚了.这是工作代码:

add_filter( 'woocommerce_thankyou', 'update_order_status', 10, 1 );

function update_order_status( $order_id ) {
  if ( !$order_id ){
    return;
  }
  $order = new WC_Order( $order_id );
  if ( 'processing' == $order->status) {
    $order->update_status( 'completed' );
  }
  return;
}
Run Code Online (Sandbox Code Playgroud)

Loi*_*tec 11

更新: 与WooCommerce版本3+的兼容性

基于:WooCommerce - 自动完成付费虚拟订单(取决于付款方式),您还可以处理条件中的所有付款方式:

// => not a filter (an action hook)
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 = new WC_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 "processing" Orders (with all others payment methods)
    elseif ( $order->has_status( 'processing' ) ) {
        $order->update_status( 'completed' );
    }
    else {
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 4

该功能woocommerce_thankyou是一个动作。您需要使用add_action函数来挂钩它。我建议将优先级更改为 ,20以便可以在 之前应用其他插件/代码更改update_order_status

add_action( 'woocommerce_thankyou', 'update_order_status', 20);
Run Code Online (Sandbox Code Playgroud)