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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36969532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:00:39  来源:igfitidea点击:

Change order status just after payment in WooCommerce

phpwordpresswoocommercepayment-gatewayorders

提问by Borys Zielonka

I need to change automatically an order status for completed after receiving payment, but only if the order status is "processing". I found that snippet, what makes orders status completed in every case, but my payments plugins after successful payment changes returns data and changes the order status for "processing". I would like to change it into "completed" after success and don't change it if the status isn't "processing". The main problem I met is I don't know how to get the received status order.

我需要在收到付款后自动更改订单状态为已完成,但前提是订单状态为“处理中”。我发现了那个片段,是什么让订单状态在每种情况下都完成,但我的付款插件在成功付款更改后返回数据并更改订单状态以进行“处理”。我想在成功后将其更改为“已完成”,如果状态不是“正在处理”,则不要更改它。我遇到的主要问题是我不知道如何获取收到的状态订单。

Here is my code:

这是我的代码:

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


Edit:

编辑:

I figured it out already. Here's the code that works for me:

我已经想通了。这是对我有用的代码:

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

回答by LoicTheAztec

Update 2- 2019: Use WooCommerce: Auto complete paid orders(updated thread)

So the right hook to use is woocommerce_payment_complete_order_statusfilter returning complete

更新 2- 2019:使用WooCommerce:自动完成支付订单(更新线程)

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



Update 1:Compatibility with WooCommerce version 3+

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

I have changed the answer

我改变了答案

Based on: WooCommerce - Auto Complete paid virtual Orders (depending on Payment methods), you will be able to handle also all payment methods in conditionals:

基于: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;
    }
}

回答by Pranav

The function woocommerce_thankyouis an action. You're required to use add_actionfunction to hook into it. I would recommend changing the priority to 20so that other plugins/code changes may be applied before update_order_status.

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

add_action( 'woocommerce_thankyou', 'update_order_status', 20);