I want to add/filter country_id column of shipping address in sales order grid.
I refer below link which is useful to display regoin_id.
By doing same I am able to add country id as a column but when I perform filter on that ambiguous error is generated.
https://www.mageworx.com/blog/how-to-add-column-with-filter-to-magento-2-orders-grid/
Please check below code, I changed for country id :
public function afterGetReport($subject, $collection, $requestName)
{
if ($requestName !== 'sales_order_grid_data_source') {
return $collection;
}
if ($collection->getMainTable() === $collection->getResource()->getTable('sales_order_grid')) {
try {
$orderAddressTableName = $collection->getResource()->getTable('sales_order_address');
$directoryCountryRegionTableName = $collection->getResource()->getTable('directory_country_region');
$collection->getSelect()->joinLeft(
['soat' => $orderAddressTableName],
'soat.parent_id = main_table.entity_id AND soat.address_type = \'shipping\'',
['telephone']
);
$collection->getSelect()->joinLeft(
['dcrt' => $directoryCountryRegionTableName],
'soat.region_id = dcrt.region_id',
['code']
);
$collection->getSelect()->joinLeft(
['dcrt' => $directoryCountryRegionTableName],
'soat.country_id = dcrt.country_id',
['country_id']
);
} catch (\Zend_Db_Select_Exception $selectException) {
// Do nothing in that case
$this->logger->log(100, $selectException);
}
}
return $collection;
}
di.xml
<column name="country_id">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
<item name="label" xsi:type="string" translate="true">Country Id</item>
<item name="sortOrder" xsi:type="number">65</item>
<item name="align" xsi:type="string">left</item>
<item name="dataType" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="filter" xsi:type="string">text</item>
</item>
</argument>
</column>