I need to filter the customers Array in

"Admin Panel -> Customers -> All Customers"

BEFORE the list is rendered, by an private attribute. The attribute is already exist.

I have 3 different sales user, those are have access to just their customers. All Customers have an attribute calleds "refer". On the php side, i need to check which user is loged in and filter customers by "refer" and display those.

Can anyone Help me please? I need to know which files I have to overwrite.

  • Specify your magento version? – Priyank May 12 '17 at 10:36
  • I use ver 2.1.5 – Done Sarl May 12 '17 at 10:39
  • use filter in retrieve query of get collection of customers, in customer model – Ronak Chauhan May 12 '17 at 10:42
  • That was also my plan. But since I'am not very familiar with magento 2.1, I need a bit more help. can you tell me which file I have to overwrite? i guess you meen the files in "vendor/magento/module-customer/Model/Customer"? – Done Sarl May 12 '17 at 10:48
  • vendor/magento/module-customer/Model/Customer/DataProvider.php -> getData()? should I modify this function? – Done Sarl May 12 '17 at 10:52

The Grid itself is an UI component and is stored in the Magento file /vendor/magento/module-customer/view/adminhtml/ui_component/customer_listing.xml.

There, it is configured to using the collection Magento\Customer\Ui\Component\DataProvider via data source named customer_listing_data_source.

To customize it as per your requirement, you may follow steps below :

Assume you are using a custom extension named "Company_MyModule".

Also, Assume the customer attribute you mention (i.e "refer") is contain admin user_id or user_mail of an admin user

step 1)

Create a di.xml under app/code/Company/MyModule/adminhtml/di.xml

File: /app/code/Company/MyModule/etc/adminhtml/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <argument name="collections" xsi:type="array">
            <item name="customer_listing_data_source" xsi:type="string">Company\MyModule\Model\ResourceModel\Customer\Grid\Collection</item>

step 2) Create the class Collection.php under /app/code/Company/MyModule/Model/ResourceModel/Customer/Grid/

File : /app/code/Company/MyModule/Model/ResourceModel/Customer/Grid/Collection.php

    namespace Company\MyModule\Model\ResourceModel\Customer\Grid;

    use Magento\Customer\Model\ResourceModel\Grid\Collection as OriginalCollection;

    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;

    class Collection extends OriginalCollection
        protected $_authSession;
        protected $logger;

        public function __construct(
           EntityFactory $entityFactory,
           Logger $logger,
           FetchStrategy $fetchStrategy,
           EventManager $eventManager,
           \Magento\Backend\Model\Auth\Session $authSession
           $this->_authSession = $authSession;
           $this->logger = $logger;
           parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager);

        protected function _renderFiltersBefore() {
            $user = $this->_authSession->getUser();
            $loggedUserId =  $user->getId();

step 3: we need to instruct the grid to display our new column. create file customer_listing.xml under app/code/Company/MyModule/view/adminhtml/ui_component/

File : app/code/Company/MyModule/view/adminhtml/ui_component/customer_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
 <columns name="customer_columns">
 <column name="refer">
 <argument name="data" xsi:type="array">
 <item name="config" xsi:type="array">
 <item name="filter" xsi:type="string">text</item>
 <item name="label" xsi:type="string" translate="true">Refer</item>

step 4: Run Following commands

sudo php bin/magento setup:di:compile sudo php bin/magento indexer:reindex

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.