在WooCommerce付款后立即更改订单状态

时间:2016-05-01 17:17:33

标签: php wordpress woocommerce payment-gateway orders

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

这是我的代码:

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;
}

编辑:

我已经弄清楚了。这是适用于我的代码:

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;
}

2 个答案:

答案 0 :(得分:9)

  

更新2 - 2019:使用AWS SDK for Python(更新的主题)

     

所以正确使用的钩子是woocommerce_payment_complete_order_status过滤器返回完整

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

我已经改变了答案

基于:WooCommerce: Auto complete paid orders,您还可以处理条件中的所有付款方式:

// => 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;
    }
}

答案 1 :(得分:4)

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

add_action( 'woocommerce_thankyou', 'update_order_status', 20);
相关问题