Search code examples
phpmagentomagento2

How to add MassAction on Order Grid in Magento2?


I want to add a MassAction in Order Grid in Magento2.

I add xml in view/adminhtml/ui_component/sales_order_grid.xml.By adding this XML MassAction is show in dropdown.But when I select the MassAction I did not get the order ids in post request.

<?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">
    <listingToolbar name="listing_top">
            <massaction name="listing_massaction">
                <action name="mass_order_assigntrackingnumber">
                    <argument name="data" xsi:type="array">
                        <item name="config" xsi:type="array">
                            <item name="type" xsi:type="string">order_assigntrackingnumber</item>
                            <item name="label" xsi:type="string" translate="true">Assign Tracking Number</item>
                            <item name="url" xsi:type="url" path="helloworld/orderprocessing/"/>
                        </item>
                    </argument>
                </action>
            </massaction>
    </listingToolbar>
</listing>

I got when I print the POST request :-

Array
(
    [key] => 2c7824dfa98ff4e9f550d2f4d07e696c650fbb48448ac3f8a8377f1c793e9294
    [excluded] => false
    [filters] => Array
        (
            [placeholder] => true
        )

    [search] => 
    [namespace] => sales_order_grid
    [form_key] => hvUs2LDFHbjJN3zY
)

Mass Action Controller

<?php
    namespace Inchoo\Helloworld\Controller\Adminhtml\Orderprocessing;
    class Index extends \Magento\Backend\App\Action{

        public function execute(){
            $data = $this->getRequest()->getParams();
            echo "<pre>";print_r($data); die;
            die("Hello");
        }
    }
?>

Please help me in to find out how to get order ids via custom MassAction in MassAction controller in Magento2 ?


Solution

  • Mass Action Controller

    Update code by this:

    <?php
    namespace Inchoo\Helloworld\Controller\Adminhtml\Orderprocessing;
    class Index extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction {
    
        protected function massAction(AbstractCollection $collection){
            //your code here
            //Here you will get collection
        }
    }
    

    Reference code: /var/www/html/mage2/vendor/magento/module-sales/Controller/Adminhtml/Order/MassCancel.php