1

I want to show orders which are assigned to the specific admin users.

If any admin user is logged in then orders which are assigned to logged-in admin user will be displayed in an Order grid.

Note: Admin users are seller which can manage their orders from admin

How can it be done using UI component?

My Seller Id column Code:

 <?xml version="1.0" encoding="UTF-8"?>

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Ui/etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">show_ecomexpress_awb.show_ecomexpress_awb_listing_data_source</item>
            <item name="deps" xsi:type="string">show_ecomexpress_awb.show_ecomexpress_awb_listing_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">show_ecomexpress_awb_columns</item>
        <item name="buttons" xsi:type="array">
            <item name="import" xsi:type="array">
                <item name="name" xsi:type="string">import</item>
                <item name="label" xsi:type="string" translate="true">Import COD AWB</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">ecomexpress/ecomexpress/fetch_fetchawb</item>
            </item>
            <item name="importppd" xsi:type="array">
                <item name="name" xsi:type="string">importppd</item>
                <item name="label" xsi:type="string" translate="true">Import PPD AWB</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">ecomexpress/ecomexpress/fetch_fetchppdawb</item>
            </item>
            <item name="flush" xsi:type="array">
                <item name="name" xsi:type="string">flush</item>
                <item name="label" xsi:type="string" translate="true">Flush AWB</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">ecomexpress/ecomexpress/deleteawb</item>
            </item>
    </item>
    </argument>
    <dataSource name="ecom_awb_listing_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Ecom\Ecomexpress\Ui\DataProvider\Ecom\AwbDataProvider</argument>
            <argument name="name" xsi:type="string">show_ecomexpress_awb_listing_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">awb_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">awb_id</item>
                    </item>
                </item>
            </argument>
        </argument>
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
            </item>
        </argument>
    </dataSource>
    <listingToolbar name="listing_top">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sticky" xsi:type="boolean">true</item>
            </item>
            <item name="buttons" xsi:type="array">
                <item name="export" xsi:type="array">
                    <item name="name" xsi:type="string">export</item>
                    <item name="label" xsi:type="string" translate="true">Export</item>
                    <item name="class" xsi:type="string">primary</item>
                    <item name="url" xsi:type="string">*/*/Export_export</item>
                </item>
            </item>
        </argument>
        <bookmark name="bookmarks"/>
        <columnsControls name="columns_controls"/>
        <filters name="listing_filters"/>
         <paging name="listing_paging" />
    </listingToolbar>
    <columns name="show_ecomexpress_awb_columns">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="storageConfig" xsi:type="array">
                    <item name="provider" xsi:type="string">show_ecomexpress_awb.show_ecomexpress_awb.listing_top.bookmarks</item>
                    <item name="namespace" xsi:type="string">current</item>
                </item>
                <item name="childDefaults" xsi:type="array">
                    <item name="fieldAction" xsi:type="array">
                        <item name="provider" xsi:type="string">show_ecomexpress_awb.show_ecomexpress_awb.show_ecomexpress_awb_columns.actions</item>
                        <item name="target" xsi:type="string">applyAction</item>
                        <item name="params" xsi:type="array">
                            <item name="0" xsi:type="string">edit</item>
                            <item name="1" xsi:type="string">${ $.$data.rowIndex }</item>
                        </item>
                    </item>
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">show_ecomexpress_awb.show_ecomexpress_awb.listing_top.bookmarks</item>
                        <item name="root" xsi:type="string">columns.${ $.index }</item>
                        <item name="namespace" xsi:type="string">current.${ $.storageConfig.root}</item>
                    </item>
                </item>
            </item>
        </argument>
         <column name="awb_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Awb Id</item>
                    <item name="sortOrder" xsi:type="number">1</item>
                </item>
            </argument>
        </column>
         <column name="awb">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Awb</item>
                    <item name="sortOrder" xsi:type="number">2</item>
                </item>
            </argument>
        </column>
         <column name="shipment_to">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Ship to</item>
                    <item name="sortOrder" xsi:type="number">3</item>
                </item>
            </argument>
        </column>
<!--          <column name="shipment_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Shipment#</item>
                    <item name="sortOrder" xsi:type="number">4</item>
                </item>
            </argument>
        </column> -->
        <actionsColumn name="shipment_id" class="Ecom\Ecomexpress\Ui\Renderer\Shipmentid">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="indexField" xsi:type="string">awb_id</item>
                    <item name="sortOrder" xsi:type="number">4</item>
                     <item name="label" xsi:type="string" translate="true">Shipment#</item>
                </item>
            </argument>     
        </actionsColumn>
<!--          <column name="state">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">State</item>
                    <item name="sortOrder" xsi:type="number">5</item>
                </item>
            </argument>
        </column> -->
        <column name="state" class="Ecom\Ecomexpress\Ui\Renderer\Awbstatus">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">State</item>
                    <item name="sortOrder" xsi:type="number">5</item>
                </item>
            </argument>
        </column>
        
        <column name="status">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Status</item>
                    <item name="sortOrder" xsi:type="number">6</item>
                </item>
            </argument>
         </column>
<!--          <column name="orderid">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Order#</item>
                    <item name="sortOrder" xsi:type="number">7</item>
                </item>
            </argument>
        </column> -->
        <actionsColumn name="orderid" class="Ecom\Ecomexpress\Ui\Renderer\Orderid">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="indexField" xsi:type="string">orderid</item>
                    <item name="sortOrder" xsi:type="number">7</item>
                     <item name="label" xsi:type="string" translate="true">Order#</item>
                </item>
            </argument>     
        </actionsColumn>
         <column name="awb_type">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">AWB Type</item>
                    <item name="sortOrder" xsi:type="number">8</item>
                </item>
            </argument>
        </column>
        <column name="distributor_code" class="Ecom\Ecomexpress\Ui\Renderer\Distributorcode">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">  </item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Distributor Code</item>
                    <item name="sortOrder" xsi:type="number">9</item>
                </item>
            </argument>
        </column>
        <column name="created_at">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Created At</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </column>
        <column name="updated_at">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Updated At</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </column>
        <column name="reverse_pickup_generated">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Reverse Pickup Generated</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </column>
        <column name="in_used">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Config\Model\Config\Source\Yesno</item>
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">60</item>
                    <item name="filter" xsi:type="string">select</item>
                    <item name="label" translate="true" xsi:type="string">In Used</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                    <item name="dataType" xsi:type="string">select</item>
                    <item name="editor" xsi:type="array">
                        <item name="editorType" xsi:type="string">select</item>
                    </item>
                </item>
            </argument>
        </column>
    </columns> 
</listing>

How can I do this?

14
  • Basically order can be display in a grid on the basis of seller-id.You mean the orders created by seller ? Commented Sep 8, 2020 at 5:18
  • No, Order assigned to the seller after order success from the frontend. Commented Sep 8, 2020 at 5:24
  • You can filter the orders collection by seller code according to the admin user session. Commented Sep 8, 2020 at 5:50
  • @SatishDubariya how can I filter order collection. and where I put this? Commented Sep 8, 2020 at 5:59
  • please share your collection file code.
    – Msquare
    Commented Sep 8, 2020 at 6:00

2 Answers 2

1

You can use code as shown below. Do not forget to override this file. vendor/magento/module-sales/Model/ResourceModel/Order/Grid/Collection.php

protected $authSession;

public function __construct(
    ...
    \Magento\Backend\Model\Auth\Session $authSession,
    ...
) {
    ...
    $this->authSession = $authSession;
    ...
}

protected function _initSelect()
{
    parent::_initSelect();
    /** admin user filter start **/
    $adminUser = $this->authSession->getUser();
    if ($adminUser->getId()) {
        $adminUserName = $adminUser->getUsername();
        $this->addFieldToFilter('seller_code', ['eq' => $adminUserName]);
    }
    /** end **/
    $tableDescription = $this->getConnection()->describeTable($this->getMainTable());
    foreach ($tableDescription as $columnInfo) {
        $this->addFilterToMap($columnInfo['COLUMN_NAME'], 'main_table.' . $columnInfo['COLUMN_NAME']);
    }

    return $this;
}

@VinodKumar - For ecomExpress Grid, you have to find your collection file and add _initSelect function in that file like below:

protected function _initSelect()
{
    parent::_initSelect();

    /** admin user filter start **/
    $adminUser = $this->authSession->getUser();
    if ($adminUser->getId()) {
        $adminUserName = $adminUser->getUsername();
        $this->addFieldToFilter('seller_code', ['eq' => $adminUserName]);
    }
    /** end **/
  
    return $this;
}
9
  • thanks, this is working fine for Order grid Commented Sep 8, 2020 at 6:38
  • If my answer is useful, you can upvote it. Thanks. Commented Sep 8, 2020 at 6:39
  • I am also looking for a custom grid ecomExpress which column added by UI Component Commented Sep 8, 2020 at 6:40
  • can you share your code? Commented Sep 8, 2020 at 6:41
  • updated code in question Commented Sep 8, 2020 at 6:45
1

Try This code into your collection file

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Sales\Model\ResourceModel\Order\Grid;

use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Psr\Log\LoggerInterface as Logger;

/**
 * Order grid collection
 */
class Collection extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult implements \Magento\Framework\Api\Search\SearchResultInterface
{
    protected $authSession;
    /**
     * Initialize dependencies.
     *
     * @param EntityFactory $entityFactory
     * @param Logger $logger
     * @param FetchStrategy $fetchStrategy
     * @param EventManager $eventManager
     * @param string $mainTable
     * @param string $resourceModel
     */
    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        \Magento\Backend\Model\Auth\Session $authSession, 
        $mainTable = 'sales_order_grid',
        $resourceModel = \Magento\Sales\Model\ResourceModel\Order::class
    ) {
        $this->authSession = $authSession;
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    }

    /**
     * @inheritdoc
     */
    protected function _initSelect()
    {
        parent::_initSelect();

        $tableDescription = $this->getConnection()->describeTable($this->getMainTable());
        foreach ($tableDescription as $columnInfo) {
            $this->addFilterToMap($columnInfo['COLUMN_NAME'], 'main_table.' . $columnInfo['COLUMN_NAME']);
        }

        return $this;
    }
    public function _beforeLoad()
    {
        $log_admin = $this->authSession->getUser()->getUserId()
        parent::_beforeLoad();
        $this->addFieldToFilter('seller_code', $log_admin);
        return $this;
    }
}
1
  • Thanks, this is working fine for order grid. I am also looking for custom grid which column added by UI Component Commented Sep 8, 2020 at 6:39

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.