0

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

2 Answers 2

0

You have to save order in last.

if ($order->getId() && Order::STATE_CANCELED !== $order->getState()) {
        $order->cancel();
        $order->addStatusHistoryComment($message);
        $order->save();
    }
5
  • It is saying that the save() method is deprecated
    – tread
    Commented Feb 16, 2017 at 12:08
  • save() needs to be used, you can refer here, Mage_Adminhtml_Sales_OrderController
    – seeni
    Commented Feb 16, 2017 at 12:21
  • @seenii, the save method should not be used. You should use \Magento\Sales\Model\OrderRepository::save() instead. If you want to cancel an order you can use \Magento\Sales\Model\Service\OrderService::cancel()
    – joni jones
    Commented Feb 16, 2017 at 16:00
  • @jonijones Please supply an answer and I will mark it
    – tread
    Commented Feb 20, 2017 at 14:35
  • @StevieG posted more detailed answer
    – joni jones
    Commented Feb 20, 2017 at 16:02
2

The model methods like save should not be used, these methods are "private" Magento code and marked as deprecated.

Magento has API interfaces, which allow working with entities as you need.

You should use \Magento\Sales\Api\OrderRepositoryInterface::save to save the entity.

In your case to change order status to cancel the \Magento\Sales\Api\OrderManagementInterface::cancel and \Magento\Sales\Api\OrderManagementInterface::addComment should be used instead.

Also, if develop a 3rd-party application, you can use Magento WEB API to work with orders:

  1. /V1/orders/:id/cancel - cancel an order.
  2. /V1/orders/:id/comments - adds comment to existing order entity.
  3. /V1/orders/ or /V1/orders/create - creates/updates order.

More details in Magento REST API documentation.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.