I want to create delete order functionality in magento2, but i am not able to add mass action in order grid. please help me how to create delete order functionality in magento2. Please help me if any one have idea.
3 Answers
You need to define router for adminhtml in order to get it work with your custom module. You can define the same at app\code{{your_package}}{{your_module}}\etc\adminhtml\routes.xml as like below:
<?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="orderdelete" frontName="orderdelete">
<module name="Krish_OrderDelete" />
</route>
</router>
</config>
You can define your own frontName for admin route. Now in your ui xml file, available at view\adminhtml\ui_component under custom mass action search the item name="url" and set path like "orderdelete/order/massDelete"
It should work if you will implement it correctly.
Please refer below module which I developed to add new MassDelete action in sales order grid (All the below files should be in your custom module i.e. package_module).
1. \etc\module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Krish_OrderDelete" setup_version="1.0.0">
<sequence>
<module name="Magento_Sales"/>
</sequence>
</module>
</config>
2. \etc\adminhtml\routes.xml
<?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="orderdelete" frontName="orderdelete">
<module name="Krish_OrderDelete" />
</route>
</router>
</config>
3. \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="order_delete">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="type" xsi:type="string">order_delete</item>
<item name="label" xsi:type="string" translate="true">Delete</item>
<item name="url" xsi:type="url" path="orderdelete/order/massDelete"/>
<item name="confirm" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Delete Order(s)</item>
<item name="message" xsi:type="string" translate="true">Are you sure you wan\'t to delete selected items?</item>
</item>
</item>
</argument>
</action>
</massaction>
</listingToolbar>
</listing>
**Note: If you will define your action under tag <listingToolbar>, than new mass action will be added as child mass action.**
4. \Controller\Adminhtml\Order\MassDelete.php
<?php
namespace Krish\OrderDelete\Controller\Adminhtml\Order;
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;
use Magento\Sales\Api\OrderManagementInterface;
/**
* Class MassDelete
*/
class MassDelete extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
{
/**
* @var OrderManagementInterface
*/
protected $orderManagement;
/**
* @param Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
* @param OrderManagementInterface $orderManagement
*/
public function __construct(
Context $context,
Filter $filter,
CollectionFactory $collectionFactory,
OrderManagementInterface $orderManagement
) {
parent::__construct($context, $filter);
$this->collectionFactory = $collectionFactory;
$this->orderManagement = $orderManagement;
}
/**
* Hold selected orders
*
* @param AbstractCollection $collection
* @return \Magento\Backend\Model\View\Result\Redirect
*/
protected function massAction(AbstractCollection $collection)
{
$countDeleteOrder = 0;
$model = $this->_objectManager->create('Magento\Sales\Model\Order');
foreach ($collection->getItems() as $order) {
if (!$order->getEntityId()) {
continue;
}
$loadedOrder = $model->load($order->getEntityId());
$loadedOrder->delete();
$countDeleteOrder++;
}
$countNonDeleteOrder = $collection->count() - $countDeleteOrder;
if ($countNonDeleteOrder && $countDeleteOrder) {
$this->messageManager->addError(__('%1 order(s) were not deleted.', $countNonDeleteOrder));
} elseif ($countNonDeleteOrder) {
$this->messageManager->addError(__('No order(s) were deleted.'));
}
if ($countDeleteOrder) {
$this->messageManager->addSuccess(__('You have deleted %1 order(s).', $countDeleteOrder));
}
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath($this->getComponentRefererUrl());
return $resultRedirect;
}
}
5. \composer.json
{
"name": "krish/magento2-order-delete",
"description": "extension for deleting orders in magento 2",
"type": "magento2-module",
"version": "1.0.0",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0",
"magento/magento-composer-installer": "*"
},
"extra": {
"map": [
[
"*",
"Krish/OrderDelete"
]
]
}
}
6. \registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Krish_OrderDelete',
__DIR__
);
-
-
@MurtuzaZabuawala, how to add tree massactions, where multi dropdown like Assign then show all the users? Mar 21, 2021 at 16:34
-
Within your module you will want to create the following file: view/adminhtml/ui_component/sales_order_grid.xml
With the following:
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Ui/etc/ui_configuration.xsd">
<container name="listing_top">
<massaction name="listing_massaction">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="actions" xsi:type="array">
<item name="delete" xsi:type="array">
<item name="type" xsi:type="string">delete</item>
<item name="label" xsi:type="string" translate="true">Delete</item>
<item name="url" xsi:type="string">sales/order/massDelete</item>
</item>
</item>
</item>
</argument>
</massaction>
</container>
</listing>
Customise the URL path in <item name="url" ..>sales/order/massDelete</item>
to your needs.
Take a look at Magento\Sales\Controller\Adminhtml\Order\MassCancel.php
for an example of how implement your controller!
-
Hello, I am ablew to add the new mass action but my controller not working when i add in our module , if i put into "Magento/Sales/Controller/Adminhtml/Order" then its working but when i add in our module then its not working . please help me how to add massaction function in our controller? Sep 15, 2015 at 10:11
-
Do you have a
etc/adminhtml/routes.xml
file within your module? You can use a post I've written covering adminhtml as a reference: ashsmith.io/magento2/module-from-scratch-part-5-adminhtml Sep 15, 2015 at 14:13 -
Hello Ash, how we can add dynamic mass action using above code Jun 7, 2016 at 17:20
-