My requirement is programatically change the order status to Canceled by using order id.

How to achieve this?


3 Answers 3


Even though using the OrderFactory would work, save and load methods are deprecated soon, you should use service contracts instead.

So you can use Magento/Sales/Api/OrderManagementInterface:

First inject an instance in your class constructor:

protected $orderManagement;

public function __construct(
    \Magento\Sales\Api\OrderManagementInterface $orderManagement,
) {
    $this->orderManagement = $orderManagement;

Then use the following:


use \Magento\Sales\Model\OrderFactory by injecting in the constructor, like below : to cancel the order programmatically

protected $_orderFactory;

public function __construct(
     \Magento\Sales\Model\OrderFactory $orderFactory
$this->_orderFactory = $orderFactory;

public function execute()
    $orderId = '1234543343'; // your order id
    $order = $this->_orderFactory->create()->load($orderId);
  • thanks, it worked but load() save() are deprecated soon as per @Raphael +1 from me :-) Commented May 13, 2016 at 11:45

I tried below solution and it works perfect


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.