Managing order statuses is critical to running a successful Magento 2 store. Sometimes, you may need to change the order status programmatically rather than manually.
This blog will guide you through the steps to Programmatically Change Order Status in Magento 2.
Whether you need to update orders based on certain conditions or automate the process, this tutorial will help you achieve your goals efficiently.
MageAnts is a certified Magento 2 development company and we’ve assisted many merchants build powerful automation into their website, and in this guide, we’ll walk you through how to do exactly that.
Let’s begin!
Steps to Change Order Status Programmatically in Magento 2
Let’s go ahead and write the code for Magento 2 to update order status programmatically.
Step 1. Load the Order
To change the order status, you first need to load it using its ID. Magento provides the OrderRepositoryInterface for this.
Here’s how you can do it:
Create the Observer file in the Observer directory:
use Magento\Sales\Api\OrderRepositoryInterface;
$orderId = 123; // Replace this with the actual order ID
$orderRepository = $objectManager->create(OrderRepositoryInterface::class);
$order = $orderRepository->get($orderId);
|
Step 2. Set the New Status
Now that you’ve loaded the order, it’s time to set the new status. You’ll use Magento's built-in setState() and setStatus() methods for this.
$newStatus = 'custom_status'; // Replace this with your custom status code
$order->setState($newStatus)->setStatus($newStatus);
|
Note: The custom_status should be a valid status that’s already configured in your Magento backend. (Stores > Settings > Order Status).
Step 3. Save the Order
After setting the new status, save the order to apply the changes.
$orderRepository->save($order);
|
And that's it! You’ve now updated an order's status programmatically.
Complete Example: Magento 2 Change Order Status Programmatically
Here's a complete example that changes the order status programmatically in Magento:
use Magento\Sales\Api\OrderRepositoryInterface;
$orderId = 123; // Replace with actual order ID
$newStatus = 'custom_status'; // Replace with your custom status code
$orderRepository = $objectManager->create(OrderRepositoryInterface::class);
$order = $orderRepository->get($orderId);
$order->setState($newStatus)->setStatus($newStatus);
$orderRepository->save($order);
|
Frequently Asked Questions
How to Add a Custom Order Status in Magento 2?
You can do this via Magento backend:
- Go to Stores > Settings > Order Status
- Click Create New Status
- Enter status code (e.g., ready_for_pickup) and status label (that will be visible on both the Admin and storefront)
- After saving, click Save Status
What Is the Difference Between “Order State” and “Order Status” in Magento 2?
- Order State means system stage (new, processing, etc.)
- Order Status means a human-readable label (Pending Payment, etc.)
How to Get Order Status in Magento 2?
You can retrieve the current order status like this:
$status = $order->getStatus();
|
Final Thoughts: Magento 2 Change Order Status Programmatically
Thank you for coming to the end of our blog. Via this article, I hope that Magento stores can now find a solution for updating their created orders status.
Magento 2 order status promised to bring great improvement in updating order status, Managing and updating orders status easier and more convenient than ever.
Let our Adobe Commerce certified professionals help you automate and optimize it for you.