Magento, Magento 2

How to Add Custom Mass Action to Product Grid in Magento 2

In this tutorial, Today I will explain to how to add custom mass action to product grid in Magento 2. In Magento 2 admin, Product grid created by ui_component. There are some mass actions already available in Magento 2 product grid. But, if you want to add custom mass action to product grid then, you need to extend product_listing.xml file.

Let’s follow the below steps to add custom mass action to product grid.

You may also like this :

1) First of all, Let’s assume that you have simple module. Now, create product_listing.xml file at app/code/RH/Helloworld/view/adminhtml/ui_component/ and paste the below code :

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<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="is_sale_products">
                <settings>
                    <type>is_sale_products</type>
                    <label translate="true">Is Salable Product</label>
                    <actions>
                        <action name="0">
                            <type>enabled</type>
                            <label translate="true">Enable</label>
                            <url path="helloworld/index/massSalable">
                                <param name="issalable">1</param>
                            </url>
                        </action>
                        <action name="1">
                            <type>disable</type>
                            <label translate="true">Disable</label>
                            <url path="helloworld/index/massSalable">
                                <param name="issalable">0</param>
                            </url>
                        </action>
                    </actions>
                </settings>
            </action>
        </massaction>
    </listingToolbar>
</listing>

2) After that, Create MassSalable.php file at app/code/RH/Helloworld/Controller/Adminhtml/Index/ and paste the below code to update value :

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\Helloworld\Controller\Adminhtml\Index;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class MassSalable extends Action
{

    /**
     * @var Filter
     */
    protected $filter;

    /**
     * @var CollectionFactory
     */
    protected $prodCollFactory;

    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @param Context                                         $context
     * @param Filter                                          $filter
     * @param CollectionFactory                               $prodCollFactory
     * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
     */
    public function __construct(
        Context $context,
        Filter $filter,
        CollectionFactory $prodCollFactory,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    ) {
        $this->filter = $filter;
        $this->prodCollFactory = $prodCollFactory;
        $this->productRepository = $productRepository;
        parent::__construct($context);
    }

    /**
     * Execute action
     *
     * @return \Magento\Backend\Model\View\Result\Redirect
     * @throws \Magento\Framework\Exception\LocalizedException | \Exception
     */
    public function execute()
    {
        $salableValue = $this->getRequest()->getParam('issalable');
        $collection = $this->filter->getCollection($this->prodCollFactory->create());
        foreach ($collection->getAllIds() as $productId) {
            $productDataObject = $this->productRepository->getById($productId);
            $productDataObject->setData('is_sale',$salableValue);
            $this->productRepository->save($productDataObject);
        }
        $this->messageManager->addSuccess(__('A total of %1 record(s) have been modified.', $collection->getSize()));
        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        return $resultRedirect->setPath('catalog/product/index');
    }
}

That’s it !!!

Now, you can set value enable/disable of “Sale” attribute value in product. You can set your custom action based on your requirement.

Output :

Product Grid Mass Action Magento 2

I hope this blog is easy to understand about how to add custom mass action to product grid in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Stay Safe and Stay Connected !!

Tagged ,