In this article we will explore how to add a mass action in admin product listing.
First you will need to define a new mass action in listing xml. We will an action - update_custom_prices.
A few things are happening here.
We are hooking into the default product_listing ui component
We are defining a new action under the massaction node
We are setting the confirmation config and message
We are defining the custom action that will be triggered when the action is selected
Next we have to define a controller. But before we can do that we need to define our a new custom admin route. We can do this by adding a new route in routes.xml.
Things to note here:
We are defining the action in admin scope
We are defining the frontName for the action - sync
Once we have our router defined we can define our controller and action. Define the controller in the directory Custom and File name is Price. This will match to our previously defined path sync/custom/price.
We are extending the core controller class for product actions Magento\Catalog\Controller\Adminhtml\Product. This is not mandatory but we are able to reuse the same ACLs as the delete and other core mass actions. We could have just as well directly extended the core Magento\Backend\App\Action class.
That is it. Once you visit the product grid you should be able to see your action and execute it.