Update woocommerce order status after the payment process is completed and redirect to the store

I use woo-commerce for my trading site. I want to update the order status to completion after payment, and then return to the 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 has been made and redirected to the payment page.

I want to change the status after the payment is completed, and then return to the redirect URL.

Here is my redirect link:

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

But when using the woocommerce_payment_complete_order_status hook woocommerce_payment_complete_order_status status does not change. The hook must be called upon completion of payment.

+5
source share
3 answers

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' ); } 
+4
source

Check out this code snippet

 add_action('woocommerce_checkout_order_processed', 'do_something'); function do_something($order_id) { $order = new WC_Order( $order_id ); // Do something } 
0
source

For the 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'; } 
0
source

Source: https://habr.com/ru/post/1233130/


All Articles