What is the correct way to change order status in Magento 2

After placing an order, my module calls an external API.

If success from external API, I need to update the order status.

I know how to update status in Magento 1 but how do you do this in Magento 2?

Thanks

Solutions

Use \Magento\Sales\Api\OrderRepositoryInterface::get and \Magento\Sales\Api\OrderRepositoryInterface::save. This is public API of sales module and after future Magento releases will not change, so your custom module will not be broken.

Magento 1 style (manipulations with models an resource models directly from third party modules) should not be used in case when suitable public API is available (such classes/methods are marked with @api tag). Such approach will lead to custom module being broken after the next Magento 2 releases.

Similar questions

How to change text of coupon code the correct way (not editing core file)
I am a front end developer trying to figure out how to override So I can change the coupon code error text, can anyone please guide me on how I would do this the correct way?
Is this a correct way change theme programatically via "layout_load_before" event Observer?
In need to change themes based on customer group. after some google I found a hook where I can made a change "layout_load_before". I am not sure this is effective solution or not. I mean here. Does this will affect theme fallback system of magento2? Does this effect to full page cache system of magento2? All I need proper explanation of doing this....
Correct way to change core module code
I have one line of code in a core module (contact) in which I need to change. What is the correct way to make the change to change/overwrite this file?
I have show this type Order ID - Order Data - Order Status and I click on order id then redireck go to order details page in magento 2?
I have show this type Order ID - Order Data - Order Status and I click on order id then redirect go to order details page ? how to do this ? exmaple order id is 2 then I click on 2 then go to order details show ?how to create this in magento 2?
Custom order status not working like magento predefined order status
I created a custom order status like below.(This is a big issue that i am simply missing some small stuff and hope to figure out with the help of a member). config.xml mysql script During the back order the order status is saved to backorders and it will show in the admin panel order list. My question is the same order is not showing in the custome...
How to get last order status history comment and order status Magento 2
I need to change the order status and order status history comment based on the last value of the status, currently i've set order status and comment status like this:

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.