Add a column to sales order grid without breaking the order's view
-
07-03-2021 - |
Question
Here is the plugin code i tried to add the columns of the custom table to the sales order grid
<?php
namespace Testing\Testorder\Plugins;
use Magento\Framework\Message\ManagerInterface as MessageManager;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as SalesOrderGridCollection;
class AddColumnsSalesOrderGridCollection
{
private $messageManager;
private $collection;
protected $request;
public function __construct(MessageManager $messageManager,
SalesOrderGridCollection $collection,
\Magento\Framework\App\Request\Http $request
) {
$this->request = $request;
$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->joinLeft(
['tt' => "Test_Testing"],
'main_table.increment_id = tt.order_id',
['username']
)->distinct();
}
}
return $this->collection;
}
but this will break the order's view page, there is an error when i click on the tabs like invoices/shipment/credit memos
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'order_id' in 'where clause', query was: SELECT COUNT(*) FROM
sales_order_grid
ASmain_table
WHERE (order_id
= '26')
the plugin also breaks "All customers"
Notice: Undefined index: website_id in /home/m22/public_html/vendor/magento/module-customer/Ui/Component/Listing/Column/Confirmation.php on line 69
Any ideas how to fix it?
Solution
Make the below changes to your plugin code. Clear your cache and reload the browser.
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->joinLeft(
['tt' => "test_testing"],
'main_table.increment_id = tt.order_id',
['username']
)->distinct();
return $this->collection;
}
}
return $result;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange