Skip to main content

How to create a custom MassAction in the Order grid of Magento 2

Sometimes our clients ask us to adapt their Magento 2 backend to meet their order flow. A simple method is to add custom MassAction. In this blog, I will explain how to achieve this in an example module.

The Scenario  

Let us assume our client has a huge number of orders. As a consequence, a lot of shipments need to be prepared and preregistered at the shipping company. The shipping company offers the possibility to upload read in a .csv with all the shipping data.  

A custom mass action in the Magento 2 Order grid is perfect for generating this .csv file. This process is more straightforward than extending the default export function or creating a custom export function.  

The module  

To create a MassAction for the Magento 2 Order grid, we require a new module. Let's create it. In this example, I called it YoungDogs ShipmentsCSVs: 

TIP: I use the free cli tool magerun2 - The swiss army knife for Magento developers to create new modules for Magento 2, with one short command:   
n98-magerun2 dev:module:create --minimal YoungDogs ShipmentsCSV 
It does the first 3 steps for you. Therefore, shortens and simplifies the process, making the life of a magento 2 developer or sysadmin much easier. 

  1. Create the folder structure inside the app/code directory of the Magento 2 installation: [image] 
  2. Add the rgistration.php (YoungDogs/ShipmentsCSV/registration.php)  
    <?php 
    use \Magento\Framework\Component\ComponentRegistrar; 
    ComponentRegistrar::register( 
     
      ComponentRegistrar::MODULE, 
         'YoungDogs_ShipmentsCSV', 
       __DIR__ 
    ); 
  3. Add the module.xml: (YoungDogs/ShipmentsCSV/etc/module.xml) 
    <?xml version="1.0"?> 
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">          <module name="YoungDogs_ShipmentsCSV" setup_version="1.0.0"> 
     </module> 
    </config> 
    TIP: I use the free cli tool Magerun2 The swiss army knife for Magento developers, sysadmins and devops">magerun2 - The swiss army knife for Magento developers to create new modules for Magento 2, with one short command:
    n98-magerun2 dev:module:create --minimal YoungDogs ShipmentsCSV&nbsp;<br />
    It does the first 3 steps for you. Therefore, shortens and simplifies the process, making the life of a magento 2 developer or sysadmin much easier.
  4. Add the sales_order_grid.xml (YoungDogs/ShipmentsCSV/view/adminhtml/ui_component/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"> 
        <listingToolbar name="listing_top"> 
                <massaction name="listing_massaction"> 
                    <action name="shipments_csv"> 
                        <argument name="data" xsi:type="array"> 
                            <item name="config" xsi:type="array"> 
                                <item name="type" xsi:type="string">shipment_csv</item> 
                                <item name="label" xsi:type="string" translate="true">Create Shipments CSV</item> 
                                <item name="url" xsi:type="url" path="shipmentscsv/order/massShipmentsCSV"/> 
                            </item> 
                        </argument> 
                    </action> 
                </massaction> 
        </listingToolbar> 
    </listing> 
     
    This adds Action "Create Shipments CSV" to the Action dropdown in the order grid  

  5. Add the route.xml (YoungDogs/ShipmentsCSV/etc/adminhtml/routes.xml) 
     
    <?xml version="1.0"?> 
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> 
        <router id="admin"> 
            <route id="shipmentscsv" frontName="shipmentscsv"> 
                <module name="YoungDogs_ShipmentsCSV" /> 
            </route> 
        </router> 
    </config> 
     
    This will route all request in the admin with 'shipmentscsv' in the uri to our modules controller for example: mystore.com/admin/shipmentscsv/order/massShipmentCSV calls the controller YoungDogs/ShipmentsCSV/Adminhtml/Order/massShipmentCSV.php  

  6. Add the Controller (YoungDogs/ShipmentsCSV/Adminhtml/Order/massShipmentCSV.php) 
    <?php 
    namespace YoungDogs\ShipmentsCSV\Controller\Adminhtml\Order; 
     
    use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; 
     
    class massShipmentCSV extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction 

        /** 
         * @var OrderManagementInterface 
         */ 
        protected $orderManagement; 
     
        /** 
         *  selected orders 
         * 
         * @param AbstractCollection $collection 
         */ 
        protected function massAction(AbstractCollection $collection) 
        { 
            $csv = array();         
            //loop through the collection of selected orders  
            foreach ( $collection as $order ) 
            { 
                // Add here your logic to fill the $csv array 
            } 
            //set headers to download the csv  
            header( 'Content-Description: File Transfer' ); 
            header( 'Content-Type: text/csv' ); 
            header( 'Content-Disposition: attachment; filename=export_shippments_' . date( 'YmdHis' ) . '.csv' ); 
            header( 'Expires: 0' ); 
            header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' ); 
            header( 'Pragma: public' ); 
     
            $handle = fopen( 'php://output', 'w' ); 
     
            foreach ( $csv as $line ) 
            { 
                fputcsv( $handle, $line, ';', '"' ); 
            } 
     
            fclose( $handle ); 
        } 

 
That’s it! 

The result is something like that:  

When clicked, the CSV containing the required information will be automatically downloaded.  

Summary:  

This is a basic mass action and should give you a picture of how you can handle the collection received in the controller 

This entry was written by Rafael our Full stack developer

-->

Also a project like this one?

Drop us a line, we are here to listen to your challenges. Let´s make success.