I added sku, country_code and product name in the order grid. All works fine but when I filter Orders by Created at it throws following error
Exception #0 (Zend_Db_Statement_Exception): SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in where clause is ambiguous, query was: SELECT COUNT(DISTINCT soa.parent_id) FROM
sales_order_grid
ASmain_table
INNER JOINsales_order_item
ASsoi
ON main_table.entity_id = soi.order_id INNER JOINsales_order_address
ASsoa
ON main_table.entity_id = soa.parent_id WHERE (created_at
>= '2017-07-31 23:00:00') AND (created_at
<= '2017-08-25 22:59:59')
My code looks like this
di.xml
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<plugin name="sales_order_additional_columns" type="Vendor\Sales\Plugin\AddColumnsSalesOrderGridCollection" />
</type>
view/adminhtml/ui-component/sales_order_grid.xml
<column name="sku">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Sku</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
</item>
</argument>
</column>
<column name="name">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Product Name</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
</item>
</argument>
</column>
and AddColumnsSalesOrderGridCollection.php
public function aroundGetReport(
\Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory $subject,
\Closure $proceed,
$requestName
) {
$result = $proceed($requestName);
if ($requestName == 'sales_order_grid_data_source') {
if ($result instanceof $collection) {
$collection->getSelect()->join(
["soi" => "sales_order_item"],
'main_table.entity_id = soi.order_id',
['sku' => 'GROUP_CONCAT(DISTINCT soi.sku)',
'name' => 'GROUP_CONCAT(DISTINCT soi.name)'
]
)->join(
["soa" => "sales_order_address"],
'main_table.entity_id = soa.parent_id',
['country_id' => 'soa.country_id']
)->group("soa.parent_id");
}
return $collection;
}
}