2

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

4
  • Add the URL to index.php to get 404 not found
    – MGPM
    Commented Jan 8, 2019 at 5:10
  • Can it possible by url rewrite? Commented Jan 8, 2019 at 5:11
  • You can redirect using .htaccess too.
    – anonymous
    Commented Jan 8, 2019 at 5:22
  • you can add in default URL rewrite as custom url(Admin Panel Menu Marketing ->SEO & Search ->URL Rewrites ) Commented Jan 8, 2019 at 6:10

3 Answers 3

7

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;
    }
}
2
  • you can add in default URL rewrite as custom url(Admin Panel Menu Marketing ->SEO & Search ->URL Rewrites ) Commented Jan 8, 2019 at 6:37
  • it will not work ? why it require code if funcnality is already there ? Commented Jan 8, 2019 at 6:37
4

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;
    }
}
0

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

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.