0

I've created a brand new WP plugin and I try to update order status with this code (I use WC 6.6.0) :

add_action( 'woocommerce_after_register_post_type', 'aapi_process' );
function aapi_process() {
  $order = wc_get_order( 'order_id' );
  $order->update_status( 'completed' );
  $order->add_order_note( 'My User Note', 1 );
}

But now my order disappears of orders admin list.

On DB, order post has completed to post_status, instead of wc-completed

When I change to wc-completed, order appears now.

update_status method on WC_Order class use set_status method on Abstract_WC_Order. This method convert status without wc- : $new_status = 'wc-' === substr( $new_status, 0, 3 ) ? substr( $new_status, 3 ) : $new_status; (line 553) and put to prop with $this->set_prop( 'status', $new_status );

Order Note work fine.

Do you have an idea ? May be wrong hook ?

------------ EDIT ------------

Work fine with init hook

add_action( 'init', 'aapi_process' );
function aapi_process() {
  $order = wc_get_order( 'order_id' );
  $order->update_status( 'completed' );
  $order->add_order_note( 'My User Note', 1 );
}
  • This question is not similar to [Woocommerce: Programmatically Updating Order Status](https://stackoverflow.com/questions/37847681/woocommerce-programmatically-updating-order-status). Status order on DB is not done properly – David C. - ProServ Jun 20 '22 at 09:14
  • You are using: `$order = wc_get_order( 'order_id' );`, however the **order_id** is not set or determined/defined.. - In brief. You don't specify which `$order` object to update – 7uc1f3r Jun 20 '22 at 11:13
  • `'order_id'` is an exemple. I give a good order id and `$order` is an instance of `WC_Order`. Order note works fine. `var_dump( $order instanceof WC_Order )` return `true` – David C. - ProServ Jun 20 '22 at 12:42
  • Works with `init` hook – David C. - ProServ Jun 20 '22 at 13:01

0 Answers0