cancel
Showing results for 
Search instead for 
Did you mean: 

Custom link in order grid not able to access custom controller but forward to order details page

Custom link in order grid not able to access custom controller but forward to order details page

I've successfully create a column in order grid page. The

sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <column name="is_erp_synced" class="Test\OrderIntegration\Ui\Component\Listing\Column\Erp">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="label" xsi:type="string" translate="true">Erp</item>
                    <item name="sortable" xsi:type="boolean">false</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Erp.php

<?php
namespace Test\OrderIntegration\Ui\Component\Listing\Column;

use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;

class Erp extends Column
{
    protected $_orderRepository;
    protected $_searchCriteria;

    public function __construct(        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        OrderRepositoryInterface $orderRepository,
        SearchCriteriaBuilder $criteria,
        array $components = [],
        array $data = []
    ) {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {

                $order  = $this->_orderRepository->get($item["entity_id"]);
                $order_id = $order->getId();
                $order_status = $order->getStatus();
                $sync_status = $order->getData("is_erp_synced");
                $html_header = "<a href='".$this->context->getUrl('test/integration/index/', ['id' => $order_id]).">";
                $html_end = "</a>";

                /*$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/erplog.log');
                $logger = new \Zend_Log();
                $logger->addWriter($writer);
                $logger->info($order->getStatus());*/




                switch ($order_status) {
                    case "pending":
                        //pending order
                        $erp_status = "Not yet";
                        break;
                    case "processing":
                        //processing order
                        if ($sync_status == 0) {
                            $erp_status =  html_entity_decode($html_header."re-sync".$html_end);
                        } else {
                            $erp_status = "Uploaded";
                        }
                        break;
                    case "Complete":
                        //canceled order
                        if ($sync_status == 0) {
                            $erp_status =  html_entity_decode($html_header."<button>re-sync</button></a>".$html_end);
                        } else {
                            $erp_status = "Uploaded";
                        }
                        break;
                    default:
                        $erp_status = "No need";
                        break;

                }

                // $this->getData('name') returns the name of the column so in this case it would return erp_status
                $item[$this->getData('name')] = $erp_status;
            }
        }

        return $dataSource;
    }
}

When i onhover the link of the href, it shows the correct controller path e.g . http://www.example.com/admin/test/integration/index/id/13/key/s2dfjksdfj123122d0dnkljdldf, but when i click it, finally it will redirect to sales order view page. http://www.example.com/admin/sales/order/view/order_id/13/

Even i change the link to www.google.com, it stills redirect me to sales order view page, anyone know what is the problem?

2 REPLIES 2

Re: Custom link in order grid not able to access custom controller but forward to order details page

Try Like this :

Replace sales_order_grid.xml code to :

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <actionsColumn name="is_erp_synced" class="Test\OrderIntegration\Ui\Component\Listing\Column\Erp">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
                    <item name="label" xsi:type="string" translate="true">ERP</item>
                    <item name="sortOrder" xsi:type="number">30</item>
                </item>
            </argument>
        </actionsColumn>
    </columns>
</listing>


And Erp.php code to :

<?php
namespace Test\OrderIntegration\Ui\Component\Listing\Column;

use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;

class Erp extends Column
{
    protected $_orderRepository;
    protected $_searchCriteria;
    protected $urlInterface;

    public function __construct(        ContextInterface $context,
                                        UiComponentFactory $uiComponentFactory,
                                        OrderRepositoryInterface $orderRepository,
                                        SearchCriteriaBuilder $criteria,
                                        \Magento\Framework\UrlInterface $urlInterface,
                                        array $components = [],
                                        array $data = []
    ) {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        $this->urlInterface = $urlInterface;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                try {
                    $order  = $this->_orderRepository->get($item["entity_id"]);
                } catch (\Exception $e) {
                    // bypassing the NoSuchEntityFound Exception
                    continue;
                }
                $order_id = $order->getId();
                $order_status = $order->getStatus();
                $sync_status = $order->getData("is_erp_synced");
                $html_header = '<a href="' . $this->urlInterface->getUrl('test/integration/index/', ['id' => $order_id]) . '" target="_blank">';
                $html_end = '</a>';
                switch ($order_status) {
                    case "pending":
                        //pending order
                        $erp_status = "Not yet";
                        break;
                    case "processing":
                        //processing order
                        if ($sync_status == 0) {
                            $erp_status =  html_entity_decode($html_header."re-sync".$html_end);
                        } else {
                            $erp_status = "Uploaded";
                        }
                        break;
                    case "complete":
                        //canceled order
                        if ($sync_status == 0) {
                            $erp_status =  html_entity_decode($html_header."<button>re-sync</button>".$html_end);
                        } else {
                            $erp_status = "Uploaded";
                        }
                        break;
                    default:
                        $erp_status = "No need";
                        break;

                }
                $item[$this->getData('name')] = $erp_status;
            }
        }
        return $dataSource;
    }
}


After that Run the cache flush commands.

Thankyou
Problem solved? Click Kudos and "Accept as Solution".


 

Re: Custom link in order grid not able to access custom controller but forward to order details page

One of the possible solutions is, you have to disable the default action on the grid row

For this, try to add this line in your sales_order_grid.xml under the config item section

<item name="fieldAction" xsi:type="boolean">false</item>

Like this

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="sales_order_columns">
<column name="is_erp_synced" class="Test\OrderIntegration\Ui\Component\Listing\Column\Erp">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="label" xsi:type="string" translate="true">Erp</item>
<item name="sortable" xsi:type="boolean">false</item>
<item name="fieldAction" xsi:type="boolean">false</item>
</item>
</argument>
</column>
</columns>
</listing>
If issue solved, Click Kudos & Accept as Solution.
LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool