【问题标题】:Change order status just after payment in WooCommerce [duplicate]在 WooCommerce 中付款后更改订单状态 [重复]
【发布时间】:2016-08-26 10:57:01
【问题描述】:

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

这是我的代码:

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

【问题讨论】:

  • 我为代码格式编辑了您的帖子,但有一个错字。 $oder_status
  • 谢谢,错字也更正了。

标签: php wordpress woocommerce payment-gateway orders


【解决方案1】:

更新 2 - 2019:使用 WooCommerce: Auto complete paid orders(更新线程)

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


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

我改了答案

基于:WooCommerce - Auto Complete paid virtual Orders (depending on Payment methods),您还可以在条件下处理所有付款方式:

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

【讨论】:

  • 谢谢,但我真的不需要检查付款方式,我只有一个。但是我遇到了一个新问题。我想更改订单状态不是在访问感谢页面之后,而是在状态更改为“处理中”的那一刻。有时会发生客户在接受付款后不回到我的商店的情况。你有什么线索吗?
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2018-01-10
    • 2019-05-18
    • 2019-02-21
    • 2020-03-15
    • 2020-11-03
    • 2016-02-22
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多