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.
saveCopyzoom_out_map<?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">
<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.
saveCopyzoom_out_map<?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.
saveCopyzoom_out_mapphp bin/magento cache:clean
Please replace placeholders like
Vendor\ModuleNamewith 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.
AI-Powered Recommended Articles
Magento 2 - How to Add a Custom Link in Admin Product Listing Grid Page
Step-by-step guide to adding a custom link to the admin product listing page in Magento 2.
Magento 2 - How to Add a Custom Filed in Order Grid
Learn how to add a custom column to the admin order grid in Magento 2 for enhanced order tracking.
How to Create UI Component Grid In Magento 2
Learn how to create a custom UI component grid in Magento 2 for enhanced administrative functionality.
How to Add Custom Admin Menu In Magento 2
Learn how to add a custom admin menu in Magento 2 for creating personalized admin sections.
How to Add Link on Store Configuration Fields Comment in Magento 2
Add links in the comments of Magento 2 store configuration fields for easier navigation and support.
How to Add Mass Action in UI Component Grid in Magento 2
Learn how to add mass actions to Magento 2 UI component grids to enhance administrative efficiency.