18

I am trying to change order status in WooCommerce, but I encountered no luck so far. $order instance is created successfully (I know it because echo $order->status; works fine, $order_id is also correct. $order->status = 'pending'; simply doesn't change anything, I do not know why.

$order = new WC_Order($order_id);
$order->status = 'pending';

Could anyone help me with this?

NakedCat
  • 852
  • 1
  • 11
  • 40

4 Answers4

65

Try this code:

$order = new WC_Order($order_id);
$order->update_status('pending', 'order_note'); // order note is optional, if you want to  add a note to order
Ratnakar - StoreApps
  • 4,261
  • 23
  • 17
  • I will check this code out tomorrow and mark this answer as correct if it works :). – NakedCat Apr 08 '14 at 18:24
  • Fatal error: Call to a member function get_order() on a non-object in /public_html/wp-content/plugins/woocommerce/includes/wc-order-functions.php on line 54 – itskawsar Jan 10 '16 at 11:10
  • @itskawsar this means the order can"t be found, probably wrong $order_id – Sami Dec 22 '17 at 08:59
9

Working with woocommerce v4.4, other answers were not working for me. I had to do it this way,

$order = wc_get_order($order_id);
$order->set_status('pending');
$order->save();

Note: Woocommerce internally adds wc prefix, you will see it if you view in the database. We do not need to explicitly add it.

Waleed
  • 141
  • 1
  • 2
6

Since Woocommerce version 3.0+ to update status you need to do this

$order = wc_get_order( $order_id );

if($order){
   $order->update_status( 'pending', '', true );
}
Maidul
  • 380
  • 3
  • 8
2

WooCommerce change order status

$order_id = 10;
$orderDetail = new WC_Order( $order_id );
$orderDetail->update_status("wc-completed", 'Completed', TRUE);

The following are the list of available status

wc-pending   For Pending payment
wc-processing   For Processing
wc-on-hold   For On hold
wc-completed   For Completed
wc-cancelled   For Cancelled
wc-refunded   For Refunded
wc-failed   For Failed
Rahul Shinde
  • 1,522
  • 1
  • 15
  • 17