:::: MENU ::::

Magento 2 mass action to admin grid

Magento 2 mass action to admin grid

Today we talk about how to add magento 2 mass action to admin grid. Like add mass action to Order gird, add mass action to product grid, add mass action to customer grid or add mass action to any custom grid. So we are going to add the mass action in order gird. You can add mass action to any other admin grid just following the example. For any kind of suggestion kindly do comments in below section of post. So first we create or duplicate the ui component file in our view/adminhtml/ui_component folder and then add our mass action into it.

Step 1: Duplicate Grid Component File

In your module create the duplicate gird file name for example order grid use sales_order_grid.xml so we create same name file our module view/adminhtml/ui_component folder we create the following file:


Step 2: Add Mass Action

Now we are going to add mass action to grid. So first we call the container listing_top then in massaction node we add reference of parent ui_coponent listing_massaction. Now we add our custom mass action.

<?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">
  <container name="listing_top">
  <massaction name="listing_massaction">
      <action name="nameofaction">
              <argument name="data" xsi:type="array">
                  <item name="config" xsi:type="array">
                      <item name="type" xsi:type="string">testing</item>
                      <item name="label" xsi:type="string" translate="true">My Label</item>
                      <item name="url" xsi:type="url" path="adminrouter/filder/controllername"/>

Step 3: Get Mass Action Values

Now getting the values in mass action to run our mass action.

 * Hello World
    * @category    QaisarSatti
    * @package     QaisarSatti_HelloWorld
    * @author      Muhammad Qaisar Satti
    * @Email       qaisarssatti@gmail.com

namespace QaisarSatti\HelloWorld\Controller\Adminhtml\Index;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;

class MassOrder extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
     * Authorization level of a basic admin session

     * @param Context $context
     * @param Filter $filter
     * @param CollectionFactory $collectionFactory

    public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
        parent::__construct($context, $filter);
        $this->collectionFactory = $collectionFactory;

     * Cancel selected orders
     * @param AbstractCollection $collection
     * @return \Magento\Backend\Model\View\Result\Redirect

    protected function massAction(AbstractCollection $collection)
        $countCancelOrder = 0;
        print_r($collection->getAllIds()); //to check the ids
        foreach ($collection->getItems() as $order) {
            //do your logic here
        $countNonCancelOrder = $collection->count() - $countCancelOrder;

        if ($countNonCancelOrder && $countCancelOrder) {
            $this->messageManager->addError(__('%1 order(s) cannot be done it .', $countNonCancelOrder));
        } elseif ($countNonCancelOrder) {
            $this->messageManager->addError(__('You done it  the order(s).'));

        if ($countCancelOrder) {
            $this->messageManager->addSuccess(__('We done it %1 order(s).', $countCancelOrder));
        $resultRedirect = $this->resultRedirectFactory->create();
        return $resultRedirect;

Following these example you can add mass action to any admin grid.

Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018

One Comment

So, what do you think ?