Magento 2 - How to Add a Custom Link in Admin Order Listing Grid Page

In this article, we'll guide you through the simple process of adding a custom link to a Magento 2 admin order listing grid page. In order to add a custom link to the admin order listing grid page in Magento 2, you need to build a custom module and use UI Components. Here's a general how-to guide to help you do that.

Before we start I assume, you have already created a custom module. If you don't have it or don't know how to create it then check out our other article How To Create a Magento 2 Module.

Step 1: Create sales_order_grid.xml at app/code/Vendor/ModuleName/view/adminhtml/ui_component/ and paste the below code. You need to change the code as per your requirement.

<?xml version="1.0" encoding="UTF-8"?> <listing xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <settings> <buttons> <button name="customlink" class="Vendor\ModuleName\Block\Adminhtml\Sales\CustomLink"/> </buttons> </settings> </listing>

In the above code, we've used the CustomLink class to add the custom button on the admin order grid page in Magento 2. Next, we're creating a CustomLink.php class file.

Step 2: Create CustomLink.php at app/code/Vendor/ModuleName/Block/Adminhtml/Sales/ and paste the below code. You need to change the code as per your requirement.

<?php namespace Vendor\ModuleName\Block\Adminhtml\Sales; use Magento\Backend\Block\Widget\Context; use Magento\Framework\AuthorizationInterface; use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface; class CustomLink implements ButtonProviderInterface { /** * @var AuthorizationInterface */ private $authorization; /** * @var Context */ private $context; /** * CustomButton constructor. * * @param AuthorizationInterface $authorization * @param Context $context */ public function __construct( AuthorizationInterface $authorization, Context $context ) { $this->authorization = $authorization; $this->context = $context; } /** * @return array */ public function getButtonData() { $message = 'Are you sure you want to process?'; return [ 'label' => __('Custom Link'), 'on_click' => "confirmSetLocation('{$message}', '{$this->context->getUrlBuilder()->getUrl('route/controller/action/')}')", 'class' => 'customlinkclass', 'sort_order' => 10 ]; } }

You can see we've used the 'route/controller/action/' as a controller, you need to replace it with your admin controller.

Step 3: Cache Clean

After making these changes, clear the Magento cache and refresh the admin order grid page to see the custom link column.

php bin/magento cache:clean

Please replace placeholders like Vendor\ModuleName with your actual namespace and module name.

Bingo!! we are done all steps.

We hope this Magento post helped you to know How to add a custom link in admin order grid page in Magento 2.

Bookmark it for your future reference. Do comment below if you have any other questions or doubts.

P.S. Do share this post with your team.

Review other articles maybe it'll help you too.

Recent Articles
Chrome Extension
Copyright © 2024 All rights reserved.
Ads OFF toggle_off