Magento2 – Why is Cancelling an Order Not Working?

magento-2.1magento2

When payment fails the following code is called:

if ($order->getId() && Order::STATE_CANCELED !== $order->getState()) {
            $order->cancel();
            $order->addStatusHistoryComment($message);
        }

When I open that order in the back the status is not set and it contains no message in the Comment History

Best Answer

You have to save order in last.

if ($order->getId() && Order::STATE_CANCELED !== $order->getState()) {
        $order->cancel();
        $order->addStatusHistoryComment($message);
        $order->save();
    }
Related Topic