Question

I want to redirect sales/order/history page to 404 in magento2.

Was it helpful?

Solution

You can do using plugin.

Create around method on execute() over class Magento\Sales\Controller\Order\History and redirect to 404 page.

Plugin Class

<?php
namespace Stackexchange\Test\Plugin;


class HistoryPlugin
{
    /**
     * @var \Magento\Framework\App\Action\Context
     */
    private $context;
    private  $response;
    private  $redirect;
    /**
     * @var \Magento\Framework\UrlInterface
     */
    private $url;


    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\UrlInterface $url
    )
    {
        $this->context = $context;
        $this->response = $context->getResponse();
        $this->redirect = $context->getRedirect();

        $this->url = $url;
    }

    public function aroundExecute(
        \Magento\Sales\Controller\Order\History $object,
        callable $proceed
    ){

        $norouteUrl = $this->url->getUrl('noroute');
        $this->getResponse()->setRedirect($norouteUrl);
        return;
    }
    /**
     * Retrieve response object
     *
     * @return \Magento\Framework\App\ResponseInterface
     */
    public function getResponse()
    {
        return $this->response;
    }
}

OTHER TIPS

Create di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Controller\Order\History">
        <plugin name="YourPluginName" type="Vendor\Module\Plugin\Order\History" />
    </type>
</config>

Create the plugin file

<?php

namespace Vendor\Module\Plugin\Order;

class History
{
    private $context;
    private $url;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
    ) {
        $this->resultRedirectFactory = $context->getResultRedirectFactory();
        $this->resultFactory = $context->getResultFactory();
        $this->url = $url;
    }

    public function aroundExecute(\Magento\Sales\Controller\Order\History $subject, \Closure $proceed)
    {
       $returnValue = $proceed();
       $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
       $norouteUrl = $this->url->getUrl('noroute');
       $result = $resultRedirect->setUrl($norouteUrl);
       return $result;
    }
}

Hope this will help you.

You need to add the following code in di.xml

<type name="Magento\Sales\Controller\Order\History">
        <plugin name="YourPluginOrderHistory"
                type="Vendor\Module\Plugin\Order\History"
                sortOrder="10"
                disabled="false"/>
    </type>

Then you need to add the following code in Vendor\Module\Plugin\Order\History History.php

<?php

namespace Vendor\Module\Plugin\Order;
use \Magento\Framework\Exception\NotFoundException;
class History
{
    public function aroundExecute(\Magento\Sales\Controller\Order\History $subject, \Closure $proceed)
    {
        $returnValue = $proceed();
        // your custom code after the original execute function
        if ($returnValue) {
            throw new NotFoundException(__('Parameter is incorrect.'));
        }

        return $returnValue;
    }
}
?>

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top