1

I created a module to add 2 columns in sales grid, shipping_information and tracking number. The columns appears but when I tried to filter by Order ID in admin order page, example with 973, I got this error

main.CRITICAL: Item (Magento\Framework\View\Element\UiComponent\DataProvider\Document\Interceptor) with the same ID "6245" already exists. {"exception":"[object] (Exception(code: 0): Item (Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\Document\\Interceptor) with the same ID \"6245\" already exists. at /home/xxxx/public_html/vendor/magento/framework/Data/Collection.php:404)"} 

Also this error

Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous

But when I tried with another ID, the error disappear

In Vendor/Module/Model/ResourceModel/Order/Grid/Collection.php

namespace Wetag\AdditionalOrderFields\Model\ResourceModel\Order\Grid;

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\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection;
use Psr\Log\LoggerInterface as Logger;

/**
 * Order grid extended collection
 */
class Collection extends OriginalCollection
{
    protected $helper;

    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'sales_order_grid',
        $resourceModel = \Magento\Sales\Model\ResourceModel\Order::class
    )
    {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    }

protected function _renderFiltersBefore()
{
    $joinTable = $this->getTable('sales_order');
    $joinTable_track = $this->getTable('sales_shipment_track');
    
    
    $this->getSelect()->joinLeft($joinTable, 'main_table.entity_id = sales_order.entity_id', ['shipping_information'])->distinct();
    $this->getSelect()->joinLeft($joinTable_track, 'sales_order.entity_id = sales_shipment_track.order_id', ['track_number'])->distinct();
    parent::_renderFiltersBefore();
}
protected function _initSelect()  {

    $this->addFilterToMap('increment_id', 'main_table.increment_id');
    $this->addFilterToMap('status', 'main_table.status');
    parent::_initSelect();
  
   return $this; 

}
}

In Vendor/Module/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\Module\Model\ResourceModel\Order\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
    <type name="Vendor\Module\Model\ResourceModel\Order\Grid\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">sales_order_grid</argument>
            <argument name="resourceModel" xsi:type="string">Magento\Sales\Model\ResourceModel\Order</argument>
        </arguments>
      </type>
</config>

In etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0">
       <sequence>
           <module name="Magento_sales" />
       </sequence>
    </module>
</config>

In Vendor/Module/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="shipping_information">
            <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 method</item>
                </item>
            </argument>
        </column>
        <column name="track_number">
            <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">Tracking number</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

I like to use the field shipping_information in order_sales_grid table, I didn’t get it without making a left join, I like to select this field without join

1 Answer 1

1

I solved the issue by adding ->group('main_table.entity_id') and delete LeftJoin to Sales_order table

protected function _renderFiltersBefore()
{
    $joinTable_track = $this->getTable('sales_shipment_track');
    
    
    $this->getSelect()->joinLeft($joinTable_track, 'main_table.entity_id = sales_shipment_track.order_id', ['track_number'])->group('main_table.entity_id');
    parent::_renderFiltersBefore();
}
2
  • Thank you for sharing I need the same functionality, may i know how we can open the popup from the order grid for same tracking information like as detail page,sorry i am new to magento Aug 17, 2020 at 11:56
  • @AkshayThakkar use can use bootstrap modal Aug 17, 2020 at 19:18

Your Answer

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

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