Welcome back Magento Folks to my blog. In this article, I will show you how we can add new custom mass action to the order grid.
The default Magento provides mass action like Cancel, Print, Delete, etc.
We can easily add new mass action with UI components by two simple steps.
- Create
sales_order_grid.xml
in custom module - Create an admin controller
Here are the description and sample code snippets for above steps.
- Create
sales_order_grid.xml
in custom moduleapp/code/MagePrince/Test/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="mass_custom_action"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="type" xsi:type="string">mass_custom_action</item> <item name="label" xsi:type="string" translate="true">Custom Order Mass Action</item> <item name="url" xsi:type="url" path="mageprince_test/massaction/customaction/"/> </item> </argument> </action> </massaction> </listingToolbar> </listing>
2. Now create an admin controller which we defined above in path="vendor_module/massaction/customaction/
<?php namespace MagePrince\Test\Controller\Adminhtml\MassAction; use Magento\Backend\App\Action; use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory; class CustomAction extends Action { const ADMIN_RESOURCE = 'Magento_Sales::sales_order'; /** * @var OrderCollectionFactory */ protected $orderCollectionFactory; /** * ChangeColor constructor. * @param Action\Context $context * @param OrderCollectionFactory $orderCollectionFactory */ public function __construct( Action\Context $context, OrderCollectionFactory $orderCollectionFactory ) { $this->orderCollectionFactory = $orderCollectionFactory; parent::__construct($context); } public function execute() { $request = $this->getRequest(); $orderIds = $request->getPost('selected', []); if (empty($orderIds)) { $this->getMessageManager()->addErrorMessage(__('No orders found.')); return $this->_redirect('sales/order/index'); } //print_r(orderIds) // Selected Order Ids $orderCollection = $this->orderCollectionFactory->create(); $orderCollection->addFieldToFilter('entity_id', ['in' => $orderIds]); try { //Add you logic foreach ($orderCollection as $order) { } } catch (\Exception $e) { $message = "An unknown error occurred while changing selected orders."; $this->getMessageManager()->addErrorMessage(__($message)); } return $this->_redirect('sales/order/index'); } }
You can implement you logic with selected order Ids in above controller.
![](https://mageprince.com/wp-content/uploads/2020/09/image-1.png)
That’s It. If you have any question feel free to comment below.
Thanks for reading. Keep sharing 🙂