Solving the Magento 2 Custom Column Export Issue
Introduction
You’ve successfully added a custom column (Tracking Number) to your Sales Order grid in Magento 2. However, when trying to export data via CSV, you’re encountering the following error:
You cannot define a correlation name 'sst' more than once
This issue arises due to a naming conflict caused by both your custom collection and the built-in sales_shipment_track table using the same correlation name (‘sst’) in their joins. Let’s resolve this by ensuring that our custom join is correctly implemented.
Understanding the Issue
The error occurs because Magento doesn’t handle multiple JOIN operations with the same correlation name ("sales_shipment_track" -> "sst") properly when exporting data to CSV. This leads to a naming conflict and subsequent errors during the export process.
Implementing the Solution
Step 1: Modify the Custom Column XML File (sales_order_grid.xml)
Ensure that your sales_order_grid.xml file is correctly configured, as shown:
<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>
Step 2: Update the Custom Collection Plugin (di.xml)
In your di.xml file, update the plugin configuration to match the following:
<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>
Step 3: Modify the Custom Collection File
Open your custom collection file (AddColumnsSalesOrderGridCollection.php) and update the aroundGetReport method as follows:
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;
}
Step 4: Clear Cache and Verify
After making these changes, clear your Magento cache using the following command:
php bin/magento cache:clean
Then, attempt to export your Sales Order data via CSV again. The error should now be resolved, and your custom column (‘Tracking Number’) will appear in the exported file.
Conclusion
By carefully managing the JOIN operations and correlation names within our custom collection, we successfully avoided the naming conflict that was causing the CSV export issue. This solution demonstrates how to properly extend Magento’s built-in functionality while maintaining data integrity across various export formats.
Remember, when working with Magento 2 extensions and plugins, always pay close attention to namespace conflicts and ensure proper configuration of your custom XML and PHP files. Clear documentation and thorough testing are essential for maintaining a stable and efficient e-commerce platform.
For more in-depth guides on Magento 2 development, feel free to explore our blog at Laravel Company.