Magento 2

How to Add New Custom Action In Sales Order - Magento 2

Magento 2

How to Add New Custom Action In Sales Order - Magento 2

Some time we needs to add custom action in Magento 2 sales order grid for fulfilling our client's requirements. 

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

Please follow bellow simple steps to Add Custom Action :

Step 1 : First, create sales_order_grid.xml file inside app\code\Milind\CustomModule\view\adminhtml\ui_component\ folder and add this code:

<?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="actions" class="Milind\CustomModule\Ui\Component\Listing\Column\ViewAction">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="indexField" xsi:type="string">entity_id</item>
                    <item name="viewUrlPath" xsi:type="string">sales/order/view</item>
                    <item name="urlEntityParamName" xsi:type="string">order_id</item>
                </item>
            </argument>
        </actionsColumn>
    </columns>
</listing>

Step 2: In this step create ViewAction.php file inside app\code\Milind\CustomModule\Ui\Component\Listing\Column\ folder and add this code:

<?php
namespace Milind\CustomModule\Ui\Component\Listing\Column;

use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;

class ViewAction extends Column
{
    protected $urlBuilder;

    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        UrlInterface $urlBuilder,
        array $components = [],
        array $data = []
    ) {
        $this->urlBuilder = $urlBuilder;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) { // check items exist or not
            foreach ($dataSource['data']['items'] as & $item) {
                if (isset($item['entity_id'])) {
                    $viewUrlPath = $this->getData('config/viewUrlPath') ?: '#';
                    $urlEntityParamName = $this->getData('config/urlEntityParamName') ?: 'entity_id';
                    $item[$this->getData('name')] = [
                        'view' => [
                            'href' => $this->urlBuilder->getUrl(
                                $viewUrlPath,
                                [
                                    $urlEntityParamName => $item['entity_id']
                                ]
                            ),
                            'label' => __('View')
                        ],
                        'reorder' => [
                            'href' => $this->urlBuilder->getUrl(
                                "sales/order_create/reorder",
                                [
                                    $urlEntityParamName => $item['entity_id']
                                ]
                            ),
                            'label' => __('Reorder')
                        ]
                    ];
                }
            }
        }
        return $dataSource;
    }
}

Now refresh the cache using below commands :

php bin/magento c:c
php bin/magento c:f

I hope your issue hence been resolve.