Question

I have a custom sales order grid that has a "view" link for each entry which takes the user to the standard Order View page.

Is there a way to make the "Back" link on the order view page go back to my custom grid, instead of the default Sales Order Grid?

EDIT: I still have the regular Sales Order Grid as well, so I need the link to return to whichever grid the user came from.

Was it helpful?

Solution

You can do this by plugin.

Create this class:

<?php
namespace NameSpace\ModuleName\Plugin\Sales\Block\Adminhtml\Order;

use Magento\Sales\Block\Adminhtml\Order\View as OrderView;
use Magento\Framework\UrlInterface;
use Magento\Framework\AuthorizationInterface;

class View
{
    /** @var \Magento\Framework\UrlInterface */
    protected $_urlBuilder;

    /** @var \Magento\Framework\AuthorizationInterface */
    protected $_authorization;

    public function __construct(
        UrlInterface $url,
        AuthorizationInterface $authorization
    )
    {
        $this->_urlBuilder = $url;
        $this->_authorization = $authorization;
    }

    public function beforeSetLayout(OrderView $view)
    {
        $url = $this->_urlBuilder->getUrl('your_route', ['order_id' => $view->getOrderId()]);
        $view->updateButton('back', 'onclick', 'setLocation(\'' . $url . '\')');
    }
}

And in your etc/adminhtml/di.xml

<?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\Block\Adminhtml\Order\View">
        <plugin name="your_module_adminhtml_order_view_add_button" type="NameSpace\ModuleName\Plugin\Sales\Block\Adminhtml\Order\View" sortOrder="10" />
    </type>
</config>

This should work.

UPDATE

You could do this:

public function beforeSetLayout(OrderView $view) {
    $param = $view->getRequest()->getParam('redirect_back'); //you can even pass your route and use it in codntion then build the back url accordingly.
    if ( $param == '1') {
        $url = $this->_urlBuilder->getUrl('your_route', ['order_id' => $view->getOrderId()]);
        $view->updateButton('back', 'onclick', 'setLocation(\'' . $url . '\')');
    }

Now, in your custom grid build URL with the redirect_back/1. So your full grid URL should be like this:

http://example.com/admin/sales/order/view/order_id/20/redirect_back/1/key/1c8e5cae74bb5d37038fbd3b79e4637caf6fd17112e40b51ef1bad63aef280b9/

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