0

I would like to "do something" (make a REST Request to a shipping company) after an order is completed (which happens on the edit order page by selecting a new status.).

Then "do something" and present the result (Success or Error) on this edit order page, maybe inside a meta box, maybe somewhere else.

I have tried this:

function do_something( $order_id ) {
  // do something
  echo "something was done";
}

add_action( 'woocommerce_order_status_completed', 'do_something', 20, 2 );

but I couldn't see any output in the edit order screen, so I think this must be coded differently. But how?

Half of the question ("write something in edit order page") has been answered here (1st answer): Display custom payment field in Woocommerce Admin, Orders and emails, like this:

function custom_field_admin_display_order_meta( $order ){
    // do something
    echo "something was done";
}

add_action(
'woocommerce_admin_order_data_after_shipping_address', 'custom_field_admin_display_order_meta', 10, 1 );

The other half of the question would be "only when status was changed to completed"...I still have no answer for that.

Thanks a lot!

Mister Woyng
  • 391
  • 2
  • 16
  • Do something using `woocommerce_order_status_completed` action hook, get the response and save the response in order meta or in transient with expiry time, then on reload check for the response meta or transient data and display your response on the page using another action hook. otherwise, you can just log the response in order notes if you can to keep track of each response. – Vijay Hardaha Nov 24 '22 at 13:58
  • @VijayHardaha Thanks for your answer...but I have not fully understood it. It's a very complicated way. – Mister Woyng Nov 25 '22 at 06:58
  • I might have a solution: check the order status with $order->get_status() before "do something", and only do it if the status is "completed". This way I can keep the woocommerce_admin_order_data_after_shipping_address hook – Mister Woyng Nov 25 '22 at 07:20

0 Answers0