2

Cancel Order from website frontend by customer in magento 2 enterprise edition.

Is there anyway to do without extensions?

2

[EDIT] Please careful when allowing the customer to cancel order on the Front page. I only want to give a hint for this issue.

In Magento 2 CE, we can use \Magento\Sales\Api\OrderManagementInterface to cancel the order.

app/code/Vendor/Module/Controller/Order/Cancel.php

<?php

namespace Vendor\Module\Controller\Order;

use Magento\Sales\Controller\OrderInterface;
use Magento\Framework\App\Action\Context;
use Magento\Sales\Controller\AbstractController\OrderLoaderInterface;
use Magento\Framework\Registry;

class Cancel extends \Magento\Framework\App\Action\Action implements OrderInterface
{
    /**
     * @var \Magento\Sales\Api\OrderManagementInterface
     */
    protected $_order;

    /**
     * @var \Magento\Sales\Controller\AbstractController\OrderLoaderInterface
     */
    protected $orderLoader;

    /**
     * @var \Magento\Framework\Registry
     */
    protected $registry;

    /**
     * Cancel constructor.
     * @param \Magento\Sales\Api\OrderManagementInterface $orderManagementInterface
     * @param Context $context
     */
    public function __construct(
        \Magento\Sales\Api\OrderManagementInterface $orderManagementInterface,
        OrderLoaderInterface $orderLoader,
        Registry $registry,
        Context $context
    )
    {
        $this->_order = $orderManagementInterface;
        $this->orderLoader = $orderLoader;
        $this->registry = $registry;
        parent::__construct($context);
    }

    public function execute()
    {
        $result = $this->orderLoader->load($this->_request);
        if ($result instanceof \Magento\Framework\Controller\ResultInterface) {
            return $result;
        }
        $order = $this->registry->registry('current_order');
        //$orderId = 1;
        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        try {
            $this->_order->cancel($order->getId());

        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            if ($this->_objectManager->get('Magento\Checkout\Model\Session')->getUseNotice(true)) {
                $this->messageManager->addNotice($e->getMessage());
            } else {
                $this->messageManager->addError($e->getMessage());
            }
        }
        return $resultRedirect->setPath('*/*/history');
    }
}

Re-use the sale route:

app/code/Vendor/Module/etc/frontend/routes.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="sales">
            <module name="Vendor_Module"/>
        </route>
    </router>
</config>

In your browser, we can call directly: http://{base_url}/sales/order/cancel/order_id/76/

Remember that we need to use Magento\Sales\Controller\AbstractController\OrderLoaderInterface for checking the authorization.

  • What is the redirect URL for cancel button in My Order page. – Amal E Thomas Aug 25 '16 at 13:35
  • I'm not sure about your idea! However, the redirect URL : return $resultRedirect->setPath('*/*/history');. – Khoa TruongDinh Aug 25 '16 at 16:41
  • @KhoaTruongDinh I have created the file as per your answer but I am getting page not found once i hit the url with order id, can you please help me on this. – Rakesh Shetty Jun 7 '17 at 9:28
  • You try to create the new route? – Khoa TruongDinh Jun 7 '17 at 9:41
  • @KhoaTruongDinh I have created app/code/Vendor/Module/Controller/Order/Cancel.php and app/code/Vendor/Module/etc/frontend/routes.xml files what else I have to do? Sorry I am new to magento. – Rakesh Shetty Jun 7 '17 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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