0

Using it on magento version 2.3.1 I guess I am missing something if someone can help would be great! I am trying to add country code column in the sales.

So far I've done the following:

  1. I have added in sales_order_grid.xml
<column name="code">
 <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">Code</item>
 </item>
 </argument>
 </column>
  1. Created folder in app/code/Vendor/ExtendGrid/Module/ResourceModel/Order/Grid/Collection.php
<?php 
    namespace Vendor\ExtendGrid\Model\ResourceModel\Order\Grid;
    use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection; use Vendor\ExtendGrid\Helper\Data as Helper;

        class Collection extends OriginalCollection {  

        protected function renderFiltersBefore()  {  
        $joinTable = $this->getTable('directory_country_region');  
        $this->getSelect()->joinLeft($joinTable, 'main_table.region_id = directory_country_region.region_id', ['code']);        parent::_renderFiltersBefore();  
    } 

} 

?>
  1. Created folder app/code/Vendor/ExtendGrid/etc/di.xml

    Vendor\ExtendGrid\Model\ResourceModel\Order\Grid\Collection

Now I can see the new column code in sales but seems like I have missed something and cannot get the date from the DB table

0

You've got to follow a tutorial like this

http://www.codextblog.com/magento-2/how-to-add-a-custom-column-in-order-grid-in-magento-2/

I've not tested this. But you should end up with something like this

app/code/Codextblog/Ordercolumn/view/adminhtml/ui_component/sales_order_grid.xml

<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" class="Codextblog\Ordercolumn\Ui\Component\Listing\Column\Mycolumn">
            <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">My Column</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

app/code/Codextblog/Ordercolumn/Ui/Component/Listing/Column/Mycolumn.php

namespace Codextblog\Ordercolumn\Ui\Component\Listing\Column;

use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;
use \Codextblog\Ordercolumn\Model\CustomFactory;

class Mycolumn extends Column
{

    protected $_orderRepository;
    protected $_searchCriteria;
    protected $_customfactory;

    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        OrderRepositoryInterface $orderRepository,
        SearchCriteriaBuilder $criteria,
        CustomFactory $customFactory,
        array $components = [], array $data = [])
    {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        $this->_customfactory = $customFactory;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $order = $this->_orderRepository->get($item["entity_id"]);
                $countryId = $order->getShippingAdress()->getCountryId()
                $item[$this->getData('country_id')] = $countryId;
            }
        }
        return $dataSource;
    }
}

app/code/Codextblog/Ordercolumn/etc/adminhtml/di.xml

<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\Reporting">
        <plugin name="sales_grid_collection" type="\Codextblog\Ordercolumn\Model\Plugin\Sales\Order\Grid"/>
    </type>
</config>

app/code/Codextblog/Ordercolumn/Model/Plugin/Sales/Order/Grid.php

namespace Codextblog\Ordercolumn\Model\Plugin\Sales\Order;


class Grid
{

    public static $table = 'sales_order_grid';
    public static $leftJoinTable = 'sales_order_address';

    public function afterSearch($intercepter, $collection)
    {
        if ($collection->getMainTable() === $collection->getConnection()->getTableName(self::$table)) {

            $leftJoinTableName = $collection->getConnection()->getTableName(self::$leftJoinTable);

            $collection
                ->getSelect()
                ->joinLeft(
                    ['soa'=>$leftJoinTableName],
                    "soa.parent_id = main_table.entity_id",
                    [
                        'country_id' => 'soa.country_id'
                    ]
                );

            $where = $collection->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE);
            $collection->getSelect()->setPart(\Magento\Framework\DB\Select::WHERE, $where);
            //echo $collection->getSelect()->__toString();die;
        }
        return $collection;
    }
}

That should get you near. Potentially I missed part about filtering shipping address only

 ->where('soa.address_type = ?', 'shipping')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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