5

Actually i had override the generic class to filter the orders for sales order grid.

<preference for="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider" type="VendorName\ModuleName\View\Element\UiComponent\DataProvider\DataProvider"/>

It create problem for me in others grid like invoice grid , Shipment grid etc

My code is there for filter order

 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();
        }
        //custom code//
        $currentUser=$this->getCurrentUserId();
        $seller_order_arr = [];
        foreach ($arrItems['items'] as $row) {
            if ($row['current_seller_id'] != null){
                if (in_array($currentUser, explode(",", $row['current_seller_id']))){
                    $seller_order_arr[] = $row;
                }
            }
        }
        $arrItems['items'] = $seller_order_arr;
        $arrItems['totalRecords']=$this->getTotalOrder($seller_order_arr);
        // end custom code//
        return $arrItems;

    }

These lines of code work perfectly order filter as i neend. But create problem for other grids. it can be solve by 2 way.

1 ) To put if() condition for custom code it should true if request come from Sales>Order and false if request come from other pages invoice or shipment how can get order page URL for making codition.

2 ) Second is don't override the generic class . Try to filter order array an others way . In Which class::method we can filter orders array like i filter inDataProvider class.

Please guide me which is better in my case and how do that . Thanks in advance

  • what is your main goal? – magefms Mar 26 at 8:07
  • 1
    Actually i make multi vendor site. i filter orders as my code show every seller can view own product order. in the quest i upload code that filter. each seller see own order not others order – HaFiz Umer Mar 26 at 8:11
  • Your code has you overriding EVERY data provider. You rarely, if ever, need to override any data provider, certainly not everyone of them. Depending on which one it is, you can just add your ModifierInterface and do what you need with minimal intrusion. – Shawn Abramson Mar 27 at 4:42
2

As per, my idea first one is not a good idea to override a class.

My suggestion to change the data provider the class of this grid .

means changes like

<dataProvider class="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider"

to

<dataProvider class="{Vendor}\{ModuleName}\Ui\Element\UiComponent\DataProvider\DataProvider"

that is the best process.

Code of New UI Data provider class:

<?php

namespace StackExchange\Magento\Ui\Element\UiComponent\DataProvider;

class DataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider {

    protected function searchResultToOutput(\Magento\Framework\Api\Search\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();
        }
        //custom code//
        $currentUser = $this->getCurrentUserId();
        $seller_order_arr = [];
        foreach ($arrItems['items'] as $row) {
            if ($row['current_seller_id'] != null) {
                if (in_array($currentUser, explode(",", $row['current_seller_id']))) {
                    $seller_order_arr[] = $row;
                }
            }
        }
        $arrItems['items'] = $seller_order_arr;
        $arrItems['totalRecords'] = $this->getTotalOrder($seller_order_arr);
        // end custom code//
        return $arrItems;
    }

}

Also override sales_order_grid.xml and past datasource tag in this file.

  • you mean to say override the vendor/magento/module-sales/view/adminhtml/ui_component/sales_order_grid.xml file and give past this line <dataProvider class="{Vendor}\{ModuleName}\Ui\Element\UiComponent\DataProvider\DataProvider ? – HaFiz Umer Mar 26 at 9:41
  • Yes it work . i override sales_order_grid.xml and past datasource tag in this file. – HaFiz Umer Mar 26 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.