Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce - change order status with php code

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?

like image 699
NakedCat Avatar asked Apr 08 '14 11:04

NakedCat


3 Answers

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
like image 96
Ratnakar - StoreApps Avatar answered Oct 03 '22 11:10

Ratnakar - StoreApps


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 );
}
like image 38
Maidul Avatar answered Oct 03 '22 11:10

Maidul


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.

like image 5
Waleed Avatar answered Oct 03 '22 12:10

Waleed