How to create an Admin tab in Magento 2 to display customers without website access, and implement it in the admin > customers section?

To create an Admin tab in Magento 2 that displays customers without website access and integrate it into the admin > customers section, you’ll need to follow these steps:

Create Block for Grid Display (Grid.php): This block extends MagentoBackendBlockWidgetGridExtended to display a grid of customers who are not approved (based on your custom attribute is_approved).

<?php
namespace JJNotApprovedCustomersBlockAdminhtml;


use MagentoBackendBlockWidgetGridExtended;
use MagentoBackendBlockTemplateContext;
use MagentoBackendHelperData as BackendHelper;
use MagentoCustomerModelResourceModelCustomerCollectionFactory;


class Grid extends Extended
{
    protected $customerCollectionFactory;


    public function __construct(
        Context $context,
        BackendHelper $backendHelper,
        CollectionFactory $customerCollectionFactory,
        array $data = []
    ) {
        $this->customerCollectionFactory = $customerCollectionFactory;
        parent::__construct($context, $backendHelper, $data);
    }


    protected function _prepareCollection()
    {
        $collection = $this->customerCollectionFactory->create();
        $collection->addAttributeToFilter([
            ['attribute' => 'is_approved', 'null' => true],
            ['attribute' => 'is_approved', 'eq' => 0]
        ]);
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }


    protected function _prepareColumns()
    {
        $this->addColumn('entity_id', [
            'header' => __('ID'),
            'sortable' => true,
            'index' => 'entity_id'
        ]);
        $this->addColumn('email', [
            'header' => __('Email'),
            'index' => 'email'
        ]);
        $this->addColumn('firstname', [
            'header' => __('First Name'),
            'index' => 'firstname'
        ]);
        $this->addColumn('lastname', [
            'header' => __('Last Name'),
            'index' => 'lastname'
        ]);


        $this->addColumn('action', [
            'header'    => __('Action'),
            'type'      => 'action',
            'getter'    => 'getId',
            'actions'   => [
                [
                    'caption' => __('Edit'),
                    'url'     => [
                        'base' => 'customer/index/edit'
                    ],
                    'field'   => 'id'
                ]
            ],
            'filter'    => false,
            'sortable'  => false,
            'index'     => 'stores',
            'is_system' => true,
        ]);


        return parent::_prepareColumns();
    }
}

Create Controller to Manage Tab Display (Index.php): This controller handles the display of the tab in the admin > customers section and sets up the customer collection based on the is_approved attribute.

<?php
namespace JJNotApprovedCustomersControllerAdminhtmlCustomer;


use MagentoBackendAppAction;
use MagentoFrameworkViewResultPageFactory;
use MagentoCustomerModelResourceModelCustomerCollectionFactory;


class Index extends Action
{
    protected $resultPageFactory;
    protected $customerCollectionFactory;


    public function __construct(
        ActionContext $context,
        PageFactory $resultPageFactory,
        CollectionFactory $customerCollectionFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->customerCollectionFactory = $customerCollectionFactory;
    }


    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->setActiveMenu('JJ_NotApprovedCustomers::notapprovedcustomers');
        $resultPage->addBreadcrumb(__('Not Approved Customers'), __('Not Approved Customers'));
        $resultPage->getConfig()->getTitle()->prepend(__('Not Approved Customers'));


        // Get the customer collection
        $customerCollection = $this->customerCollectionFactory->create();
        $customerCollection->addAttributeToFilter([
            ['attribute' => 'is_approved', 'null' => true],
            ['attribute' => 'is_approved', 'eq' => 0]
        ]);


        // Pass the collection to the layout
        if ($block = $resultPage->getLayout()->getBlock('notapprovedcustomers_grid')) {
            $block->setCollection($customerCollection);
        } else {
            throw new Exception('Block not found');
        }


        return $resultPage;
    }
}

Layout XML to Add Admin Tab (view/adminhtml/layout/notapprovedcustomers_index_index.xml): This XML file defines the layout updates to add your custom tab to the admin > customers section.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="JJNotApprovedCustomersBlockAdminhtmlGrid" name="notapprovedcustomers_grid"/>
        </referenceContainer>
    </body>
</page>


Leave a comment

Your email address will not be published. Required fields are marked *