简体   繁体   English

付款过程完成后更新woocommerce订单状态并重定向到商店

[英]update woocommerce order status after payment process complete and redirect to store

I am using woo-commerce for my shopping site.我正在为我的购物网站使用 woo-commerce。 I want to update the order status to complete after payment was made and then return to a success page.我想在付款后更新订单状态以完成,然后返回成功页面。

I used the following code:我使用了以下代码:

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

function my_change_status_function ($order_status, $order_id) {
  $order = new WC_Order($order_id);
  return 'completed';
}

But this function is called before the payment was made and redirects to the payment page.但是这个函数在支付之前被调用并重定向到支付页面。

I want to change the status after the payment was completed and then return to redirect URL.我想在支付完成后更改状态,然后返回redirect URL。

Here is my redirect link:这是我的重定向链接:

http://example.com/checkout/order-received/82/?key=wc_order_5614e28c9d183&state=return

But the status is not changing when I use the woocommerce_payment_complete_order_status hook.但是当我使用woocommerce_payment_complete_order_status钩子时,状态并没有改变。 The hook should be called after the payment is completed.支付完成后应调用钩子。

Try using the following code in your plugin尝试在您的插件中使用以下代码

add_action( 'woocommerce_payment_complete', 'my_change_status_function' );

function my_change_status_function( $order_id ) {

    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );

}

Check out this piece of code看看这段代码

add_action('woocommerce_checkout_order_processed', 'do_something');

function do_something($order_id) { 
   $order = new WC_Order( $order_id ); 
   // Do something
}

For Cash On Delivery method, this worked for me:对于货到付款方法,这对我有用:

add_filter( 'woocommerce_cod_process_payment_order_status', 'prefix_filter_wc_complete_order_status', 10, 3 );

function prefix_filter_wc_complete_order_status( $status, $order ) {
    return 'on-hold';
}

For most other methods, this worked:对于大多数其他方法,这有效:

add_filter( 'woocommerce_payment_complete_order_status', 'prefix_filter_wc_complete_order_status', 10, 3 );

function prefix_filter_wc_complete_order_status( $status, $order_id, $order ) {
     return 'on-hold';
}

Working with WooCommerce v4.4, other answers were not working for me.使用 WooCommerce v4.4,其他答案对我不起作用。 I had to do it this way: https://stackoverflow.com/a/64285242/7198947我不得不这样做: https : //stackoverflow.com/a/64285242/7198947

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM