Question

I need to show only pending order by users I follow this link

They used sales_order_grid_collection_load_before in magento 1, what exact event in magento 2

and

How to Collect only pending Orders from Order collection and Show for specific users.

Was it helpful?

Solution

Yes, I achieved this Solution via Plugin.

di.xml:

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
      <plugin name="sales_order_pending" type="Vendor\Module\Plugin\CustomSalesOrderGridCollection" sortOrder="100" />
</type>

CustomSalesOrderGridCollection.php

          <?php namespace Vendor\Module\Plugin;

    use Magento\Framework\Message\ManagerInterface as MessageManager;
    use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as SalesOrderGridCollection;

    class CustomSalesOrderGridCollection
    {
        private $messageManager;
        private $collection;
        protected  $adminSession;
        protected $logger;

        public function __construct(MessageManager $messageManager,
            SalesOrderGridCollection $collection,
            \Magento\Backend\Model\Auth\Session $adminSession,
            \Psr\Log\LoggerInterface $logger
        ) {

            $this->messageManager = $messageManager;
            $this->collection = $collection;
            $this->adminSession = $adminSession;
            $this->logger = $logger;
        }

        public function aroundGetReport(
            \Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory $subject,
            \Closure $proceed,
            $requestName
        ) {
           $current_adminuser =   $this->adminSession->getUser()->getAclRole();
           $this->logger->addDebug("admin user");
           $this->logger->addDebug($current_adminuser);

           if(12 == $current_adminuser){

             $result = $proceed($requestName);
             if ($requestName == 'sales_order_grid_data_source') {
                 if ($result instanceof $this->collection) {

                     $this->collection->addFieldToFilter('status', array('in' => array('pending')));
                 }

             }
           }
            return $this->collection;
        }
    }

After Clear Cache and Login that user, then its will show only pending Order in sale_order_grid.

OTHER TIPS

I was facing same issue and finally I resolved the issue by following code. You can use following code with your customisation.

First you need to create a plugin.

di.xml

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
      <plugin name="sales_order_grid_collection" type="VENDOR\MODULE\Plugin\CustomSalesOrderGridCollection" sortOrder="100" />
     </type>

Create plugin file in VENDOR/MODULE/plugin/CustomSalesOrderGridCollection.php

CustomSalesOrderGridCollection.php

<?php namespace VENDOR\MODULE\Plugin;

use Magento\Framework\Message\ManagerInterface as MessageManager;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as SalesOrderGridCollection;

class CustomSalesOrderGridCollection
{
    private $messageManager;
    private $collection;
    protected  $adminSession;

    public function __construct(MessageManager $messageManager,
        SalesOrderGridCollection $collection,
        \Magento\Backend\Model\Auth\Session $adminSession
    ) {

        $this->messageManager = $messageManager;
        $this->collection = $collection;
        $this->adminSession = $adminSession;            
    }

    public function aroundGetReport(
        \Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory $subject,
        \Closure $proceed,
        $requestName
    ) {
        $result = $proceed($requestName);
        if ($requestName == 'sales_order_grid_data_source') {
             $current_adminuser =   $this->adminSession->getUser()->getAclRole();
             if(12 == $current_adminuser){
                  if ($result instanceof $this->collection) {
                      $this->collection->addFieldToFilter('status', array('in' => array('pending')));
                  }
              }
             return $this->collection;
        }
        return $result;

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top