14
$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?

Jackson
9,98933 gold badges132 silver badges217 bronze badges
asked Mar 16, 2017 at 6:09

6 Answers 6

28

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

Ashish Raj
1,3752 gold badges15 silver badges32 bronze badges
answered Mar 16, 2017 at 6:21
9
  • thanks for reply @Dhaval Drcsystems i want update order status only Commented 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. Commented Mar 16, 2017 at 6:28
  • the above code shows \Magento\Sales\Model\Order not found error Commented Nov 24, 2017 at 12:32
  • Can you please show me your file ? Commented Nov 27, 2017 at 3:41
  • How to update custom order status? If i have status called 'sent_to_print'? Commented Aug 15, 2018 at 11:17
16

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);
answered Mar 16, 2017 at 7:02
4
  • great thanks for reply @calypso but i want to update only order status Commented Mar 16, 2017 at 7:16
  • @Rahulocodewire It is also in comment. Commented Mar 16, 2017 at 7:19
  • @ calypso please explain where i put this code in my module Commented Mar 16, 2017 at 12:53
  • how i show pending status in controller ? Commented Mar 23, 2017 at 13:26
12

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

Prince Patel
23.1k10 gold badges102 silver badges124 bronze badges
answered Jul 11, 2018 at 14:03
1
  • how to update custom order status? Commented Aug 15, 2018 at 12:54
7

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.

answered Sep 24, 2018 at 22:14
2
  • 1
    incremental id is like #3000001469-1, not 1234. 1234 is rather order id Commented Mar 29, 2019 at 9:06
  • Thanks for pointing that out @Greck. Updated the response. Commented Apr 2, 2019 at 15:49
4

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

answered Oct 1, 2018 at 10:04
1

If you are looking for updating order status via GraphQL then this change in resolver would help.

use Magento\Sales\Api\OrderManagementInterface;

use Magento\Sales\Api\OrderRepositoryInterface;

public function __construct(
 \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
 \Magento\Sales\Api\OrderManagementInterface $orderManagement,
 \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface $orderStatusRepository
) {
 $this->orderRepository = $orderRepository;
 $this->orderManagement = $orderManagement;
 $this->orderStatusRepository = $orderStatusRepository;
}
 
 $this->orderManagement->cancel($orderData['orderid']);
 $order->setData('state', "cancel");
 $order->setStatus("cancel");
answered May 23, 2022 at 7:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.