3

How can i filter the sale order grid by any preference , observer or plugin . How override sales order grid.

Can any one help me . I just want to apply condition just like addFeildToFilter and i pass custom_attribute with value and then just selected order record shown in grid.

Thanks in advance

2

Fortunately am giving the answer of my own question. i spend 3 Days on this question i don't want to spend time any other developer .

I override the function which return the array or Collection that display the orders on Sales->Order page on admin site.

make di.xml file in "app\code\VendorName\ModuleName\etc\di.xml"

and paste the code given below

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <preference for="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider" type="VendorName\ModuleName\View\Element\UiComponent\DataProvider\DataProvider"/>
</config>

Now make the DataProvider.php File in such a way : "VendorName\ModuleName\View\Element\UiComponent\DataProvider\DataProvider.php"

Paste the code given below.

<?php
namespace VendorName\ModuleName\View\Element\UiComponent\DataProvider;

use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\ReportingInterface;
use Magento\Framework\Api\Search\SearchCriteria;
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;

    class DataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
    {
        /**
         * Data Provider name
         *
         * @var string
         */
        protected $name;

        /**
         * Data Provider Primary Identifier name
         *
         * @var string
         */
        protected $primaryFieldName;

        /**
         * Data Provider Request Parameter Identifier name
         *
         * @var string
         */
        protected $requestFieldName;

        /**
         * @var array
         */
        protected $meta = [];

        /**
         * Provider configuration data
         *
         * @var array
         */
        protected $data = [];

        /**
         * @var ReportingInterface
         */
        protected $reporting;

        /**
         * @var FilterBuilder
         */
        protected $filterBuilder;

        /**
         * @var SearchCriteriaBuilder
         */
        protected $searchCriteriaBuilder;

        /**
         * @var RequestInterface
         */
        protected $request;

        /**
         * @var SearchCriteria
         */
        protected $searchCriteria;
        protected $seller_order_arr;
        /**
         * @param string $name
         * @param string $primaryFieldName
         * @param string $requestFieldName
         * @param ReportingInterface $reporting
         * @param SearchCriteriaBuilder $searchCriteriaBuilder
         * @param RequestInterface $request
         * @param FilterBuilder $filterBuilder
         * @param array $meta
         * @param array $data
         */
        public function __construct(
            $name,
            $primaryFieldName,
            $requestFieldName,
            ReportingInterface $reporting,
            SearchCriteriaBuilder $searchCriteriaBuilder,
            RequestInterface $request,
            FilterBuilder $filterBuilder,
            array $meta = [],
            array $data = []
        )
        {
            $this->request = $request;
            $this->filterBuilder = $filterBuilder;
            $this->name = $name;
            $this->primaryFieldName = $primaryFieldName;
            $this->requestFieldName = $requestFieldName;
            $this->reporting = $reporting;
            $this->searchCriteriaBuilder = $searchCriteriaBuilder;
            $this->meta = $meta;
            $this->data = $data;
            $this->prepareUpdateUrl();
        }
        protected function searchResultToOutput(SearchResultInterface $searchResult)
        {
            $arrItems = [];

            $arrItems['items'] = [];
            foreach ($searchResult->getItems() as $item) {
                $itemData = [];
                foreach ($item->getCustomAttributes() as $attribute) {
                    $itemData[$attribute->getAttributeCode()] = $attribute->getValue();
                }
                $arrItems['items'][] = $itemData;
                $arrItems['totalRecords'] = $searchResult->getTotalCount();
        }
                    return $arrItems;
        }
            public function getSearchCriteria()
            {
                if (!$this->searchCriteria) {
                    $this->searchCriteria = $this->searchCriteriaBuilder->create();
                    $this->searchCriteria->setRequestName($this->name);
                }
                return $this->searchCriteria;
            }

            /**
             * Get data
             *
             * @return array
             */
            public
            function getData()
            {
                return $this->searchResultToOutput($this->getSearchResult());
            }

            /**
             * Get config data
             *
             * @return array
             */


            /**
             * Set data
             *
             * @param mixed $config
             * @return void
             */

            /**
             * Returns Search result
             *
             * @return SearchResultInterface
             */

              public function getSearchResult()
            {
                return $this->reporting->search($this->getSearchCriteria());
            }

    }

In the above code $arrItems['items'] return the array that contains all order collection You can Filter easily.

In my case i save the custom_arribute in sales_order_grid table and i want to filter rows or collection on the behalf on this custom_attribute

This my Code is 100% Perfect. Try this and save you important Time.

Befor run code should be compile php bin/magento setup:di:compile

  • how will show grid in front end – prabhakaran7 Feb 23 at 6:20
  • Filter , Edit or Customize the $arrItems['items'] array and return it will be automatically set in grid in your frond end admin page. – HaFiz Umer Feb 23 at 10:38
  • $arrItems['items'] Can you show the example items for this plzz – prabhakaran7 Feb 23 at 10:42
  • i don't understand what you want to see ? i think you say show what i am filter or customize order grid ? – HaFiz Umer Feb 23 at 10:45
  • $arrItems['items'] here which items i can use expample Order id, Order status – prabhakaran7 Feb 23 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.