1

I am trying to show data of order/sales by country into admin grid, so I am joining two tables sales_order_grid and sales_order_address in my Collection.php

Here is my code

 use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
 use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
 use Magento\Framework\Event\ManagerInterface as EventManager;
 use Magento\Eav\Model\Entity\Collection\AbstractCollection;
 use Psr\Log\LoggerInterface as Logger;
 use Magento\Framework\App\ResourceConnection;

 class Collection extends \Magento\Sales\Model\ResourceModel\Order\Grid\Collection 
{
protected $_resource;
/**
 * Initialize dependencies.
 *
 * @param EntityFactory $entityFactory
 * @param Logger $logger
 * @param FetchStrategy $fetchStrategy
 * @param EventManager $eventManager
 * @param string $mainTable
 * @param string $resourceModel
 */
public function __construct(
    EntityFactory $entityFactory,
    Logger $logger,
    AbstractCollection $_resource,
    FetchStrategy $fetchStrategy,
    EventManager $eventManager,
    $mainTable = 'sales_order_grid',
    $resourceModel = \Magento\Sales\Model\ResourceModel\Order::class
) {
    parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    $this->_resource = $_resource;
}

/**
 * @inheritdoc
 */
protected function _initSelect()
{
    parent::_initSelect();

    $tableDescription = $this->getConnection()->describeTable($this->getMainTable());
    $tableDescription->addAttributeToSelect('*');
    $joinConditions = 'e.customer_id = s.customer_id';
    $tableDescription->addAttributeToSelect('*');
    $tableDescription->getSelect()->join(
        ['s' => 'sales_order_address'],
        $joinConditions,
        []
    )->columns("s.country_id");
    foreach ($tableDescription as $columnInfo) {
        $this->addFilterToMap($columnInfo['COLUMN_NAME'], 'main_table.' . $columnInfo['COLUMN_NAME']);
    }
    return $this;
}

its still showing only sales_order_grid's column, country_id still blank in my admin grid.

1

1 Answer 1

3
<?php

namespace Krish\CustomAdmin\grid;

class Collection extends \Magento\Sales\Model\ResourceModel\Order\Grid\Collection
{
    protected function _initSelect()
    {
        parent::_initSelect();

        $this->getSelect()->joinInner(
            ['secondTable' => $this->getTable('sales_order_address')],
            "main_table.entity_id = secondTable.parent_id AND secondTable.address_type = 'shipping'"
        );
        $this->addFilterToMap('country_id', 'country_id');
        return $this;
    }
}

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.