Include Discount Column in admin sales order grid without adding field in sales_order_grid table

Hello Friends

Today I am going to explain how you can include discount columns in admin order grid without adding field in sales_order_table
If you are learner and want to learn magento in depth you can join my youtube channel.
                                 
First of all you need to create Module , I hope you know how to create module if you don’t know you can
Now create app/code/Mageacademy/Sales/Model/ResourceModel/Order/Grid/Collection.php File
and include below code
<?php
 
namespace Mageacademy\Sales\Model\ResourceModel\Order\Grid;
 
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection;
use Psr\Log\LoggerInterface as Logger;
 
/**
* Order grid extended collection
*/
class Collection extends OriginalCollection
{
protected $helper;
 
public function __construct(
EntityFactory $entityFactory,
Logger $logger,
FetchStrategy $fetchStrategy,
EventManager $eventManager,
$mainTable = ‘sales_order_grid’,
$resourceModel = \Magento\Sales\Model\ResourceModel\Order::class
)
{
parent::__construct($entityFactory$logger$fetchStrategy$eventManager$mainTable$resourceModel);
}
 
protected function _renderFiltersBefore()
{
$joinTable = $this->getTable(‘sales_order’);
$this->getSelect()->joinLeft($joinTable‘main_table.entity_id = sales_order.entity_id’, [‘tax_amount’‘discount_amount’]);
parent::_renderFiltersBefore();
}

}

Now Create app/code/Mageacademy/Sales/etc/di.xml
and add below code
<type name=Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory>
<arguments>
<argument name=collections xsi:type=array>
<item name=sales_order_grid_data_source xsi:type=string>Mageacademy\Sales\Model\ResourceModel\Order\Grid\Collection</item>
</argument>
</arguments>
</type>
<type name=Mageacademy\Sales\Model\ResourceModel\Order\Grid\Collection>
<arguments>
<argument name=mainTable xsi:type=string>sales_order_grid</argument>
<argument name=resourceModel xsi:type=string>Magento\Sales\Model\ResourceModel\Order</argument>
</arguments>

</type>

 
Now create app/code/Mageacademy/Sales/view/adminhtml/ui_component/sales_order_grid.xml
and add below code
 
<?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>
<columns name=sales_order_columns>
<column name=tax_amount class=Magento\Sales\Ui\Component\Listing\Column\PurchasedPrice>
<argument name=data xsi:type=array>
<item name=config xsi:type=array>
<item name=filter xsi:type=string>textRange</item>
<item name=label xsi:type=string translate=true>Tax</item>
</item>
</argument>
</column>
<column name=discount_amount class=Magento\Sales\Ui\Component\Listing\Column\PurchasedPrice>
<argument name=data xsi:type=array>
<item name=config xsi:type=array>
<item name=filter xsi:type=string>textRange</item>
<item name=label xsi:type=string translate=true>Discount</item>
</item>
</argument>
</column>
</columns>

</listing>       

 
Now run php bin/magento setup:upgrade command to activate your module and check in admin
you willl see discount column as below
Happy Coding!
Leave a Reply
Your email address will not be published. *