5

I have added a tracking number(custom field) column in sales order grid. It is displaying in sales order grid and export by XML also working fine. But while I export the data by csv only the following error has occurred.

You cannot define a correlation name 'sst' more than once

The following steps I have implemented in the custom module.

Added the custom column into app/code/Vendor/Modulename/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="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>

And override the sales_order_additional_columns function by plugin in app/code/Vendor/Modulename/etc/di.xml file

<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">
 <plugin name="sales_order_additional_columns" type="Vendor\Modulename\Plugins\AddColumnsSalesOrderGridCollection" sortOrder="100" disabled="false" />
 </type>
</config>

Overrite the Collection of sales order grid file. app/code/Vendor/Modulename/Plugins/AddColumnsSalesOrderGridCollection.php

<?php 
namespace Vendor\Modulename\Plugins;
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(
 ["sst" => "sales_shipment_track"],
 'main_table.entity_id = sst.entity_id',
 'sst.track_number'
 )
 ->distinct(); 
 }
 }
 return $this->collection;
 }
}
asked May 9, 2018 at 7:26
1

2 Answers 2

5

join is happening twice in process of exporting, to avoid it, add checking in registry:

use Magento\Framework\Message\ManagerInterface as MessageManager;
use Magento\Framework\Registry;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as SalesOrderGridCollection;
 class AddColumnsSalesOrderGridCollection
 {
 private $messageManager;
 private $collection;
 private $registry;
 public function __construct(MessageManager $messageManager,
 SalesOrderGridCollection $collection,
 Registry $registry
 )
 {
 $this->messageManager = $messageManager;
 $this->collection = $collection;
 $this->registry = $registry;
 }
 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
 ) {
 if (is_null($this->registry->registry('shipment_joined'))) {
 $select = $this->collection->getSelect();
 $select->join(
 ["sst" => "sales_shipment_track"],
 'main_table.entity_id = sst.entity_id',
 'sst.track_number'
 )
 ->distinct();
 $this->registry->register('shipment_joined', true);
 }
 }
 }
 return $this->collection;
 }
 } 
answered May 9, 2018 at 20:49
2
  • I have an issue something related to this , I need to export the order csv in that i have to show the subtotal including the discount amount(if coupon is aplayed ). here is my question magento.stackexchange.com/questions/235101/… Commented Jul 19, 2018 at 9:07
  • Here you have used registry and now the registry is deprectaed. Moreover around plugin may call twice so please use after plugin in it. Commented Jul 21, 2021 at 12:01
2
use Magento\Framework\Message\ManagerInterface as MessageManager;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as SalesOrderGridCollection;
class AddColumnsSalesOrderGridCollection
{
 /**
 * @param \Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory $subject
 * @param SalesOrderGridCollection $collection
 * @param $requestName
 * @return mixed
 */
 public function afterGetReport($subject, $collection, $requestName)
 {
 if ($requestName == 'sales_order_grid_data_source') {
 if ($result instanceof $collection) {
 $select = $collection -> getSelect();
 $select->join(
 ["sst" => "sales_shipment_track"],
 'main_table.entity_id = sst.entity_id',
 'sst.track_number'
 )->distinct();
 }
 }
 return $collection;
 }
 
} 
answered Aug 4, 2021 at 6:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.