$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?
6 Answers 6
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
-
thanks for reply @Dhaval Drcsystems i want update order status onlyRahul Katoch– Rahul Katoch2017年03月16日 06:26:32 +00:00Commented 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.Dhaval Solanki– Dhaval Solanki2017年03月16日 06:28:17 +00:00Commented Mar 16, 2017 at 6:28
-
the above code shows \Magento\Sales\Model\Order not found errorJaisa– Jaisa2017年11月24日 12:32:00 +00:00Commented Nov 24, 2017 at 12:32
-
Can you please show me your file ?Dhaval Solanki– Dhaval Solanki2017年11月27日 03:41:34 +00:00Commented Nov 27, 2017 at 3:41
-
How to update custom order status? If i have status called 'sent_to_print'?Jafar Pinjar– Jafar Pinjar2018年08月15日 11:17:05 +00:00Commented Aug 15, 2018 at 11:17
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 statusRahul Katoch– Rahul Katoch2017年03月16日 07:16:59 +00:00Commented Mar 16, 2017 at 7:16
-
@Rahulocodewire It is also in comment.gelanivishal– gelanivishal2017年03月16日 07:19:39 +00:00Commented Mar 16, 2017 at 7:19
-
@ calypso please explain where i put this code in my moduleRahul Katoch– Rahul Katoch2017年03月16日 12:53:29 +00:00Commented Mar 16, 2017 at 12:53
-
how i show pending status in controller ?Rahul Katoch– Rahul Katoch2017年03月23日 13:26:22 +00:00Commented Mar 23, 2017 at 13:26
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– Jafar Pinjar2018年08月15日 12:54:45 +00:00Commented Aug 15, 2018 at 12:54
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.
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
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");