13
$orderId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId); 
$order->setState("processing")->setStatus("processing");
$order->save();

Please tell me how I update the order status in Magento 2?

5 Answers 5

26

You can do it like following way

declare below namespace

use Magento\Sales\Model\Order;

$orderId = 1;
//Use constructor injection for Order class. Below $objectManager is just for testing/explanation purpose
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
$orderState = Order::STATE_PROCESSING;
$order->setState($orderState)->setStatus($orderState);
$order->save();

Hope this helps

9
  • thanks for reply @Dhaval Drcsystems i want update order status only Mar 16, 2017 at 6:26
  • You can refer this model so you will get better idea Magento\Sales\Model\Order\Payment Check here how they are processing order with code. Mar 16, 2017 at 6:28
  • the above code shows \Magento\Sales\Model\Order not found error
    – Jaisa
    Nov 24, 2017 at 12:32
  • Can you please show me your file ? Nov 27, 2017 at 3:41
  • How to update custom order status? If i have status called 'sent_to_print'? Aug 15, 2018 at 11:17
15

Order state update

To update the order state and and status pro-grammatically in order define the status and state in this format. Initiate order object in the construct function and use that order object in the custom function to update it. To update order state programmatically in model, get order object from the construct function.

public function __construct(
    \Magento\Sales\Model\Order $order
){
    $this->order = $order;
}
public function updateorder(){
    $order = $this->order;
    $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING, true);
    $order->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING);
    $order->addStatusToHistory($order->getStatus(), 'Order processed successfully with reference');
    $order->save();
}

New order email

$orderid = '10000000';
$order = $this->_objectManager->get('Magento\Sales\Model\Order')->loadByIncrementId($orderid);
$this->_objectManager->get('Magento\Sales\Model\Order\Email\Sender\OrderSender')->send($order);
5
  • great thanks for reply @calypso but i want to update only order status Mar 16, 2017 at 7:16
  • @Rahulocodewire It is also in comment. Mar 16, 2017 at 7:19
  • @ calypso please explain where i put this code in my module Mar 16, 2017 at 12:53
  • how i show pending status in controller ? Mar 23, 2017 at 13:26
  • @gelanivishal working perfectly thank you Jun 1, 2020 at 15:17
8

Since load() and save() are deprecated now (because :)

* @deprecated 100.1.0 because entities must not be responsible for their own loading.
* Service contracts should persist entities. Use resource model "load" or collections to implement
* service contract model loading operations.

I used the OrderRepositoryInterface to load and save the order :

use Magento\Sales\Api\OrderRepositoryInterface;

public function __construct(
    OrderRepositoryInterface $orderRepository,
    ...
) {
    $this->orderRepository = $orderRepository;
}

...
$order = $this->orderRepository->get($orderId);
$order->setState(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW);
$order->setStatus(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW);

try {
    $this->orderRepository->save($order);
} catch (\Exception $e) {
    $this->logger->error($e);
    $this->messageManager->addExceptionMessage($e, $e->getMessage());
}

Hope it helps

1
  • how to update custom order status? Aug 15, 2018 at 12:54
4

Thanks to @medmek and @prince-patel answer. As question from @jafar-pinjar regarding custom order status, setState and setStatus calls can take on Status Code. For example, custom status code "paid" is created. To update the status/state to an order:

...
use \Magento\Sales\Api\OrderRepositoryInterface;

class nameOfTheClass {
    ...
    protected $_orderRepository;
    ...
    public function __construct(..., OrderRepositoryInterface $orderRepository, ...){

        $this->_orderRepository = $orderRepository;
        ...
    }
    ...
    public function setOrderStatus($orderID, $statusCode){
        try{
            // obtain the order with the order ID
            $order = $this->_orderRepository->get($orderID);
            $order->setState($statusCode)->setStatus($statusCode);
            $this->_orderRepository->save($order);
            return true;
        } catch (\Exception $e){
            // add some logging here
            return false;
        }
    }
    ...
}

To update the order status:

$orderID = 1234; // this is the order ID
$code = 'paid';
$this->setOrderStatus($orderID, $code);

Hope that helps someone out there.

2
  • 1
    incremental id is like #3000001469-1, not 1234. 1234 is rather order id
    – Greck
    Mar 29, 2019 at 9:06
  • Thanks for pointing that out @Greck. Updated the response.
    – qgor
    Apr 2, 2019 at 15:49
3

Magento 2.2.2 This only works the following way!

    $order = $this->order->loadByIncrementId('000000001');
    //$order = $this->order;
    $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING, true)->save();
    $order->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING, true)->save();
    $order->addStatusToHistory($order->getStatus(), 'Order processed successfully with reference again and again');
    //$order->save();

But the only problem that I have is that it doesn't updates fully status : enter image description here

It only updates the in the grid and the follow: enter image description here

Also not possible to create new invoice,shipping and credit memo. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.