We have added some additional sortable / filterable columns to sales order grid using some basic joins. This would be the relevant code regarding data
app/code/Vendor/Module/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<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">Vendor\Module\Model\ResourceModel\Order\Grid\Collection</item>
</argument>
</arguments>
</type>
<type name="Sinapsis\OrderGrid\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>
</config>
And then app/code/Vendor/Module/Model/ResourceModel/Order/Grid/Collection.php
<?php
namespace Vendor\Module\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;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OrderGridCollection;
use Psr\Log\LoggerInterface as Logger;
class Collection extends OrderGridCollection
{
public function __construct(
EntityFactory $entityFactory,
Logger $logger,
FetchStrategy $fetchStrategy,
EventManager $eventManager,
$mainTable = 'sales_order_grid',
$resourceModel = Order::class
) {
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
}
protected function _renderFiltersBefore()
{
$joinTable = $this->getTable('sales_order');
$this->getSelect()->joinLeft(
['sales_order_table' => $joinTable],
'main_table.entity_id = sales_order_table.entity_id',
['coupon_code']
);
$joinTable = $this->getTable('sales_order_address');
$this->getSelect()->joinLeft(
['sales_order_address_table' => $joinTable],
'sales_order_table.billing_address_id = sales_order_address_table.entity_id',
['country_id', 'region', 'city', 'postcode', 'telephone']
);
parent::_renderFiltersBefore();
}
}
Now we want to add a column including some product details, such as product name, sku, etc... In this case, being complex data, it's not required to be sortable but it should be filterable
How could we do the join to sales_order_item
database table, so we can concatenate all possible results in one single field per order with the products info?