Hi by default Magento don't provide the cancel order from front-end . But its a great feature to have on the website to make customer more trust on the website .
To achieve this you can create your custom controller and follow the below code.In this i am posting a order id from the account page to my controller
<?php
Nmaespace\Modulename\Controller\Action;
class Cancelorder extends \Magento\Framework\App\Action\Action
{
protected $orderManagement;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Sales\Api\OrderManagementInterface $orderManagement
) {
$this->orderManagement = $orderManagement;
parent::__construct($context);
}
public function execute()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if(!$customerSession->isLoggedIn()) {
$this->_redirect('/');
die;
}
/*get request params */
$get_customer_id = $customerSession->getCustomer()->getId();
$get_order_id = $this->getRequest()->getParam('order_id');
/*get request params */
//die;
$order = $objectManager->create('Magento\Sales\Model\Order')->load($get_order_id);
$getcustomerid = $get_customer_id;
$orderdata = $order->getData();
$order_status = $orderdata["status"];
//print_r($orderdata);
$cus_id = $orderdata["customer_id"];
if($getcustomerid != $cus_id){
echo "We cant Cancel this order at this time" ;
//die("go back");
}
if($order_status == "pending"){
$this->orderManagement->cancel($get_order_id);
echo "Order Cancelled successfully" ;
}
else{
echo "We cant Cancel this order at this time" ;
}
}
}
thanks. Hope this will help .