Cancelling order in magento 2 programatically:
I implemented the module in magento 1 in which I will cancel the pending order from customer my account page.
Please refer to the code below which I implemented in the magento 1:
$order = Mage::getModel('sales/order')->load($orderId);
$order_status=$order->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save();
I want to implement the same functionality in magento 2. Does anyone know how to implement it?
I loaded the order with order id in magento 2, but am unable to cancel the order.
You should use API (more about magento2 API concepts) for that, example how to use it in your class: