WooCommerce - change order status with php code

后端 未结 3 753
有刺的猬
有刺的猬 2020-12-15 09:16

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;

相关标签:
3条回答
  • 2020-12-15 10:01

    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 );
    }
    
    0 讨论(0)
  • 2020-12-15 10:02

    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
    
    0 讨论(0)
  • 2020-12-15 10:17

    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.

    0 讨论(0)
提交回复
热议问题