Requirement:-
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 admin user, those are have access to just their customers. All Customers have an attribute called “manager_id”. On the php side, i need to check which user is loged in and filter customers by “manager_id” and display those.
Solution:-
We need to display the customer data in the customer grid. 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 “Thecoachsmb_MyModule“.
Also, Assume the customer attribute you mention (i.e “manager_id“) is contain admin user_id or user_mail of an admin user
Step 1:
Create a di.xml under app/code/Thecoachsmb/MyModule/etc/di.xml
File: /app/code/Thecoachsmb/MyModule/etc/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">
<arguments>
<argument name="collections" xsi:type="array">
<item name="customer_listing_data_source" xsi:type="string">Thecoachsmb\MyModule\Model\ResourceModel\Customer\Grid\Collection</item>
</argument>
</arguments>
</type>
</config>
Step 2:
Create the class Collection.php under /app/code/Thecoachsmb/MyModule/Model/ResourceModel/Customer/Grid/
File : /app/code/Thecoachsmb/MyModule/Model/ResourceModel/Customer/Grid/Collection.php
<?php
namespace Thecoachsmb\MyModule\Model\ResourceModel\Customer\Grid;
use Magento\Customer\Model\ResourceModel\Grid\Collection as CusCollection;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Psr\Log\LoggerInterface as Logger;
class Collection extends CusCollection{
protected $_authSession;
protected $logger;
private $localeResolver;
public function __construct(
EntityFactory $entityFactory,
Logger $logger,
FetchStrategy $fetchStrategy,
EventManager $eventManager,
ResolverInterface $localeResolver,
\Magento\Backend\Model\Auth\Session $authSession)
{
$this->_authSession = $authSession;
$this->logger = $logger;
$this->localeResolver = $localeResolver;
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager);
}
protected function _renderFiltersBefore() {
$user = $this->_authSession->getUser();
$loggedUserId = $user->getId();
$this->getSelect()->where('refer='.$loggedUserId);
parent::_renderFiltersBefore();
}
}
?>
Step 3:
we need to instruct the grid to display our new column. create file customer_listing.xml under app/code/Thecoachsmb/MyModule/view/adminhtml/ui_component/
File : app/code/Thecoachsmb/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="manager_id">
<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">Manager Id</item>
</item>
</argument>
</column>
</columns>
</listing>
step 4: Run Following commands
php bin/magento setup:di:compile
php bin/magento indexer:reindex
php bin/magento cache:flush
These 4 simple steps will help you to filter the Customer Grid with the specific condition.
Please feel free to connect for any help.
Do comment below giving feedback. I will definitely appreciate.
Happy Learning !!
Thank You !!