1

I tried to show the shipping country in the Admin Sales Order Grid but nothing works.

I did check the customer address configuration twice and there I can see that the country variable exists and should be shown. But in the grid you can not see the shipping country.

Any workarounds?

ecommerce
  • 91
  • 11

1 Answers1

3

I found a solution. Creating a Module and Rewrite Class.

app/code/Vendor/ExtendedAdminGrid/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\ExtendedAdminGrid\Model\ResourceModel\Order\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

app/code/Vendor/ExtendedAdminGrid/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_ExtendedAdminGrid" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Sales"/>
            <module name="Magento_Backend"/>
        </sequence>
    </module>
</config>

app/code/Vendor/ExtendedAdminGrid/Model/ResourceModel/Order/Grid/Collection.php

<?php
namespace Vendor\ExtendedAdminGrid\Model\ResourceModel\Order\Grid;

class Collection extends \Magento\Sales\Model\ResourceModel\Order\Grid\Collection
{
    protected function _renderFiltersBefore()
    {
        $this->getSelect()->joinLeft(
            ["soa" => "sales_order_address"],
            "main_table.entity_id = soa.parent_id and soa.address_type = 'shipping'",
            array('country_id')
        )
        ->distinct();

        parent::_renderFiltersBefore();
    }
    protected function _initSelect()
    {

        $this->addFilterToMap('created_at', 'main_table.created_at');
        $this->addFilterToMap('base_grand_total', 'main_table.base_grand_total');
        $this->addFilterToMap('grand_total', 'main_table.grand_total');
        $this->addFilterToMap('store_id', 'main_table.store_id');
        $this->addFilterToMap('store_name', 'main_table.store_name');
        $this->addFilterToMap('order_id', 'main_table.order_id');
        $this->addFilterToMap('order_increment_id', 'main_table.order_increment_id');
        $this->addFilterToMap('billing_name', 'main_table.billing_name');
        $this->addFilterToMap('billing_name', 'main_table.shipping_name');
        $this->addFilterToMap('status', 'main_table.status');

        parent::_initSelect();
    }
}

app/code/Vendor/ExtendedAdminGrid/view/adminhtml/ui_component/sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <column name="country_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Shipping Country ID</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

app/code/Vendor/ExtendedAdminGrid/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_ExtendedAdminGrid',
    __DIR__
);

Thanks to Sergey for this great blogpost Modifying the default magento2 sales order grid — adding a coupon code column.

ecommerce
  • 91
  • 11