I have an observer in my module the fires when the order is placed, depending on a condition I use
$order->setState(\Magento\Sales\Model\Order::STATE_HOLDED, true)->save();
$order->setStatus(\Magento\Sales\Model\Order::STATE_HOLDED, true)->save();
To set the order status to "on hold", this works but when I try to take the order off hold from the admin it says "the order was not on hold" even though the status is on hold in both sales_order and sales_order_grid
I have also tried with object manager which does the exact same thing (also, the comment is added so it seems like its right):
$orderId = $orderIncrementId;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order')->load($order_id);
$currentStatus = $order->getState();
$status = 'holded';
$comment = 'Age verification failed';
$isNotified = false;
$order->setState($status);
$order->setStatus($status);
$order->addStatusToHistory($order->getStatus(), $comment);
$order->save();
And I have tried through the order repository (but this didn't work as it's in the observer sales_order_save_after but still says "order id does not exist)
$holdStatus= \Magento\Sales\Model\Order::STATE_HOLDED;
$id = $orderIncrementId;
try{
$order = $this->orderRepository->get($id);
$order->setStatus($holdStatus)->setState($holdSatus);
$this->orderRepository->save($order);
} catch (NoSuchEntityException $ex) {
alert("not worked");
}
Currently, I'm manually changing the order status to processing in the database to get the orders off hold but as we up-scale the module this won't be practical - does anyone have any idea about this before I create a button that manually changes the status in sales_order and sales_order_grid with direct SQL?