In this article, I will explain and demonstrate how we can add mass actions to a product listing page on the backend. Some already exist, like deleting a product or changing its status.
Table of Contents
Use of Mass action
The image below shows some of the default mass actions available. These actions help an admin user avoid opening and changing each product’s status. He can directly select the products using the existing filters and delete or change their status accordingly.
Adding a new action
Sometimes the existing functions available in Magento 2 don’t solve the problem, and you need to modify it to work according to your requirements. Such as adding a new mass action, so let’s start the development process.
I will be using an example where we will be sending product details to a marketplace using the mass action created in the catalog product grid page. Firstly we need a module; if there is an existing one, then you can use it, or you can create one by following our article on how to create one from here. Create Magento 2 Custom Module Development. Apart from the module, we will also need routes.xml for the admin controller. The own module name will be Learningmagento_Massprocess.
routes.xml
The routes.xml file needs to be created inside Learningmagento/Massprocessetc/adminhtml/routes.xml since we will be working on the admin part. The ID and front name will keep the same. We will also need to add our module name inside the route tag, which is Learningmagento_Massprocess.
<?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="marketplace" frontName="marketplace">
<module name="Learningmagento_Massprocess"/>
</route>
</router>
</config>
product_listing.xml
This is the important file that we need to be added to add our mass action inside the grid. The location for this file will be inside Learningmagento/Massprocess/view/adminhtml/ui_component/product_listing.xml. After creating this file, add the below code inside it.
<?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="market">
<settings>
<confirm>
<message translate="true">Do you want to update products on Marketplace?</message>
<title translate="true">Update items</title>
</confirm>
<url path="marketplace/marketprocess/updateproduct"/>
<type>marketplace</type>
<label translate="true">Update on Marketplace</label>
</settings>
</action>
</massaction>
</listingToolbar>
</listing>
Originally the file comes from the Magento_Catalog module, which is the module for the products and categories. This file has been created in many different modules to add more mass actions, new columns, and more things to the grid. Since we are adding our mass action to the product grid, we will be making changes under the element listingToolbar, then followed by massaction and finally we are adding our action as “market”. We are adding the confirmation message to show to our admin users. Under Url we are passing the controller path, which is Route front name/Controller name/Action name. Lastly, we are adding the label, which will be shown when we click on the mass action drop-down.
Controller class
The last thing that we are adding is a controller action class that will handle the request sent from the product listing page when the user selects some product and clicks the “Update on Marketplace” mass action. As per the URL and the route, we will create the class under Learningmagento/Massprocess/Controller/Adminhtml/Marketplaceprocess/Updateproduct.php
<?php
namespace Learningmagento\Massprocess\Controller\Adminhtml\Marketprocess;
use Magento\Backend\App\Action\Context;
class Updateproduct extends \Magento\Backend\App\Action
{
protected $productCollection;
protected $filter;
protected $marketplaceHelper;
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection,
\Magento\Ui\Component\MassAction\Filter $filter,
\Learningmagento\Massprocess\Helper\marketplaceHelper $helper,
Context $context
) {
$this->productCollection = $productCollection;
$this->filter = $filter;
$this->marketplaceHelper = $helper;
parent::__construct($context);
}
public function execute()
{
try {
$collection = $this->filter->getCollection($this->productCollection->create());
if($collection->getSize() == 0) {
throw new \Magento\Framework\Exception\LocalizedException(__('No products found.'));
}
$status = $this->marketplaceHelper->updateProduct($collection);
if($status) {
$this->messageManager->addSuccessMessage(__("Product updated successfully on Marketplace."));
}
else {
throw new \Magento\Framework\Exception\LocalizedException(__('Something went wrong while update products.'));
}
catch (\Exception $e) {
$this->messageManager->addErrorMessage("Product update failed on marketplace : " . $e->getMessage());
}
$this->_redirect('*/*/index');
}
}
The controller class will get the product collection using the Filter class. The collection will consist of all the products that were selected by the admin. We are using a helper class that will be in our module. I am not going to write the code for this class, as it will increase the article length; also, the main focus here is on adding the mass action.