How to Change Order Status Programmatically in Magento 2
The Magento 2 orders have an order status that is linked with a state in the order processing flow.
One can create custom order status in Magento 2 if required.
For example, a custom order status “printed”. When the admin prints out the packing slip, the order status is changed to “printed”.
Instead of doing it manually, one can change order status programmatically in Magento 2.
Method to Change Order Status Programmatically in Magento 2
In the code given below, change status in “Status Code” as per your business requirements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php use Magento\Sales\Model\Order; protected $order; public function __construct(Order $order) { $this->order = $order; } public function orderStatusChange() { $orderId = 9999; $order = $this->order->load($orderId); $order->setStatus("Status Code"); $order->save(); } |
That’s it.
If you need help with this solution, feel free to ask in the Comments section below.
I would be happy to solve your queries.
Please do consider sharing this post to Magento Community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Solved: PHP Fatal Error Class ‘locale’ Not Found in Magento 2
Solved: inventory_stock_1 Doesn’t Exist in Magento 2
Next