1

I have save custom_attribute(current_seller_id) in sales_order and sales_order_grid and same values in both table's columns.For save value in sales_order code is given below .I have use event sales_model_service_quote_submit_before that dispatch order and quote

public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getData('order');
        $quote = $observer->getData('quote');
        $quoteItems = $quote->getItems();
        foreach ($quoteItems as $quoteItem){
            if ($quoteItem->getCurrentSellerId() != ""){
                $items[]=$quoteItem->getCurrentSellerId();
            }
        }
        $seller_ids=implode(',',$items);
        $order->setCurrentSellerId($seller_ids);
        return $this;
    }

Now i want to apply condition(Filter) on the basis of own custom_attribute(current_seller_id).For example if the seller that has current_seller_id is 2 then just show those record in order grid which has value 2. I will get current admin id via "\Magento\Backend\Model\Auth\Session $authSession"

1
  • 1
    am waiting answer for this question. Nobody is there to answer my question ?
    – HaFiz Umer
    Commented Feb 20, 2019 at 13:57

1 Answer 1

1

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 getTotalOrder($ordersize){
         return  sizeof($ordersize);
        }
        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());
        }

}
1
  • hello, I have similar requirement. Can you please explain where you use that filed ? I need to filter with customer email. Base on login user need to display particular order only that match with current user email. Commented Apr 28, 2020 at 12:52

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.