1

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 AS main_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?

3
  • The error message posted above states that there is no column called "order_id" in "sales_order_grid" table. In "sales_order_grid" only "entity_id" field refers the order id. Also i don't understand in the Plugin Code why you are comparing increment_id with order_id both are different. Eg:- increment id will look like "000000001" and order id is what appears in the query parameter "sales/order/view/order_id/26/key"
    – mydons
    Jul 17, 2019 at 5:49
  • @mydons the order_id is in my custom table Test_Testing
    – hkguile
    Jul 17, 2019 at 7:06
  • @mydons even i've change order_id -> something_id, the errors are exists
    – hkguile
    Jul 17, 2019 at 7:44

1 Answer 1

1

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;
}
1
  • Thanks! it works perfectly
    – hkguile
    Jul 17, 2019 at 13:21

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.