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

14

You can do it like following way

declare below namespace

use Magento\Sales\Model\Order;

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

Hope this helps

  • thanks for reply @Dhaval Drcsystems i want update order status only – Rahul Katoch Mar 16 '17 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. – Dhaval Solanki Mar 16 '17 at 6:28
  • the above code shows \Magento\Sales\Model\Order not found error – Jaisa Nov 24 '17 at 12:32
  • Can you please show me your file ? – Dhaval Solanki Nov 27 '17 at 3:41
  • How to update custom order status? If i have status called 'sent_to_print'? – jafar pinjar Aug 15 '18 at 11:17
10

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

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

  • how to update custom order status? – jafar pinjar Aug 15 '18 at 12:54
3

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.

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

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.