How to create custom grid like sales order gird with new,edit,save,delete,status,massdelete..etc in magento2.?
Magento – How to create custom grid like sales order in magento2
magento2order-gridsales-order
Related Solutions
As Magento save coupon code
on Sales_flat_Order table so you need to MySQL join with Sales_flat_order
table
Add sales_flat_order table to collection.In magento, sales_flat_order and sales_flat_order_grid table related with Flat Grid order table entity_id and sales_flat_order table entity_id
.
$select = $collection->getSelect();
$select->joinLeft(array('order' => Mage::getModel('core/resource')->getTableName('sales/order')), 'order.entity_id=main_table.entity_id',
array('coupon_code' => 'coupon_code'))
Rewrite class then add columns to show Coupon:
<?php
/**
* Sales Order Class
*
* @author Amit Bera
*/
class Amit_CustomOrderGrid_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
protected function _prepareColumns()
{
$this->addColumn('coupon_code', array(
'header' => Mage::helper('sales')->__('Color Name'),
'align' =>'left',
'width' => '50px',
'index' => 'coupon_code',
'filter_index' => 'order.coupon_code'
));
parent::_prepareColumns();
return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();
}
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(array('order' => Mage::getModel('core/resource')->getTableName('sales/order')),
'order.entity_id=main_table.entity_id',array('coupon_code' => 'coupon_code'))
$this->setCollection($collection);
// return parent::_prepareCollection();
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
}
?>
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__
);
Best Answer
Without more information I would recommend to take a look into simple tutorials as Tutorial Ashsmith. It was an enormous help for me. More information you might find in core modules that already have grids with filters etc implemented or in the official DevDocs