Skip to content

Magento2 join product details in sales order grid as filterable column

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="MagentoFrameworkViewElementUiComponentDataProviderCollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="sales_order_grid_data_source" xsi:type="string">VendorModuleModelResourceModelOrderGridCollection</item>
            </argument>
        </arguments>
    </type>
    <type name="SinapsisOrderGridModelResourceModelOrderGridCollection">
        <arguments>
            <argument name="mainTable" xsi:type="string">sales_order_grid</argument>
            <argument name="resourceModel" xsi:type="string">MagentoSalesModelResourceModelOrder</argument>
        </arguments>
    </type>
</config>

And then
app/code/Vendor/Module/Model/ResourceModel/Order/Grid/Collection.php

<?php
namespace VendorModuleModelResourceModelOrderGrid;

use MagentoFrameworkDataCollectionDbFetchStrategyInterface as FetchStrategy;
use MagentoFrameworkDataCollectionEntityFactoryInterface as EntityFactory;
use MagentoFrameworkEventManagerInterface as EventManager;
use MagentoSalesModelResourceModelOrder;
use MagentoSalesModelResourceModelOrderGridCollection as OrderGridCollection;
use PsrLogLoggerInterface 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?