0

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/

enter image description here

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>
1
  • Did you get any further with this?
    – elfling
    Jan 14, 2021 at 17:27

1 Answer 1

1

If it is no collection

change

return $collection;

to

return $result

Full working code for country_id would be as follows

use Magento\Framework\Message\ManagerInterface as MessageManager;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as SalesOrderGridCollection;

class AddColumnsSalesOrderGridCollection
{
    private $messageManager;
    private $collection;

    public function __construct(MessageManager $messageManager,
                                SalesOrderGridCollection $collection
    ) {

        $this->messageManager = $messageManager;
        $this->collection = $collection;
    }

    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 $this->collection
            ) {
                $select = $this->collection->getSelect();

                $select->join(
                    ["soa" => "sales_order_address"],
                    'main_table.entity_id = soa.parent_id AND soa.address_type="shipping"',
                    array('country_id')
                )
                    ->distinct();
                return $this->collection;
            }

        }
        return $result;
    }



}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.