5

I am using woo-commerce for my shopping site. 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.

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. The hook should be called after the payment is completed.

Till Helge
  • 9,253
  • 2
  • 40
  • 56
himanshu archirayan
  • 223
  • 1
  • 5
  • 18
  • does your filter fired ? when order completed? if not try change `add_filter` to `add_action`, Check these links for more infor [link](https://wordpress.org/support/topic/woocommerce-on-order-complete-insert-quantity-data-into-custom-database-table) , [Link 2](http://www.rcorreia.com/woocommerce/woocommerce-automatically-set-order-status-payment-received/) – Noman Oct 07 '15 at 09:46
  • @Noman yes filter fired, but before payment made. it firing while site is transfering to payment page. i want it ti fire after payment complete. – himanshu archirayan Oct 07 '15 at 09:58
  • so you need to add this code in `order-received` page , it will fire when the page is in view. – Noman Oct 07 '15 at 10:01
  • @Noman `order-received` is default woocommerce page. when i edit that page i only see shortcode for `checkout` page. not any custom created template. – himanshu archirayan Oct 07 '15 at 10:05
  • in `functions.php` `if(is_page('order-received')) { add_filter( 'woocommerce_payment_complete_order_status', 'my_change_status_function', 10, 2 ); }` add this condition – Noman Oct 07 '15 at 10:13
  • @Noman i am working with plugin, so its not good to add anything in functions.php file is there any way where we can do it through plugin. even your given condition is not working in functions.php file – himanshu archirayan Oct 07 '15 at 10:34

4 Answers4

5

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' );

}
Lyle Bennett
  • 90
  • 1
  • 9
Anand Shah
  • 14,575
  • 16
  • 72
  • 110
0

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
}
Nedim
  • 563
  • 1
  • 11
  • 23
0

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';
}
Adal
  • 606
  • 7
  • 12
0

Working with WooCommerce v4.4, other answers were not working for me. I had to do it this way: https://stackoverflow.com/a/64285242/7198947

Waleed
  • 141
  • 1
  • 2