1

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.

1 Answer 1

2

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/

5
  • Thanks for this. It looks like it will change the back link in all cases? I should have been clearer, I also still have the regular Sales Order Grid as well, so basically I need the back link to return to whichever grid the user came from.
    – Geat
    Commented Oct 19, 2020 at 23:22
  • That will be a little tricky to achieve. Why not just adding a new button instead? You can add a new button from the same plugin. Commented Oct 19, 2020 at 23:29
  • I'd like to avoid confusing the user if possible, minimizing the number of available options.
    – Geat
    Commented Oct 19, 2020 at 23:42
  • 1
    Check my update. Commented Oct 20, 2020 at 0:46
  • It works flawlessly - perfect solution.
    – Geat
    Commented Oct 20, 2020 at 18:13

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.