We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

Create the below files and add the code in your custom module

Create the registration.php file to the following path in the app/code/Magemonkeys/CustomerAttr/registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
	MagentoFrameworkComponentComponentRegistrar::MODULE,
	'Magemonkeys_CustomerAttr',
	__DIR__
);
Create the module.xml file to the following path in the app/code/Magemonkeys/CustomerAttr/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magemonkeys_CustomerAttr" setup_version="1.0.0">
    </module>
</config>
Create MyCustomerAttribute.php file to the following path in the app/code/Magemonkeys/CustomerAttr/Setup/Patch/Data
<?php
namespace MagemonkeysCustomerAttrSetupPatchData;
 
use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoFrameworkSetupPatchPatchRevertableInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerSetupCustomerSetup;
use MagentoEavModelEntityAttributeSetFactory;
 
class MyCustomerAttribute implements DataPatchInterface, PatchRevertableInterface
{
 
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;
    /**
     * @var CustomerSetup
     */
    private $customerSetupFactory;
 
    /**
     * Constructor
     *
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $customerSetupFactory,
        SetFactory $attributeSetFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }
 
    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        /** @var $attributeSet Set */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(
            MagentoCustomerModelCustomer::ENTITY,
            'pan_number',
            [
                'type' => 'varchar',
                'label' => 'Pan Number',
                'input' => 'text',
                'source' => '',
                'required' => false,
                'visible' => true,
                'position' => 500,
                'system' => false,
                'backend' => ''
            ]
        );
         
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'customer_pan_no')->addData([
            'is_used_in_grid' => true,
            'is_filterable_in_grid' => true,
            'is_searchable_in_grid' => true,
            'used_in_forms' => [
                'adminhtml_customer'
            ]
        ]);
        $attribute->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId

        ]);
        $attribute->save();
 
        $this->moduleDataSetup->getConnection()->endSetup();
    }
 
    public function revert()
    {
        $this->moduleDataSetup->getConnection()->startSetup();
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $customerSetup->removeAttribute(MagentoCustomerModelCustomer::ENTITY, 'pan_number');
        $this->moduleDataSetup->getConnection()->endSetup();
    }
 
    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
 
    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [
         
        ];
    }
}
Create CustomerPancolumn.php file to the following path in the app/code/Magemonkeys/CustomerAttr/Ui/Component/Listing/Column
<?php
namespace MagemonkeysCustomerAttrUiComponentListingColumn;

use MagentoFrameworkViewElementUiComponentContextInterface;
use MagentoFrameworkViewElementUiComponentFactory;
use MagentoUiComponentListingColumnsColumn;
use MagentoCustomerModelCustomerFactory;

class CustomerPancolumn extends Column
{
    /**
     *
     * @param ContextInterface   $context
     * @param UiComponentFactory $uiComponentFactory
     * @param array              $components
     * @param array              $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        CustomerFactory $customerFactory,
        array $components = [],
        array $data = []
    ) {
        $this->customerFactory = $customerFactory;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }
    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item)
            {
                $customer = $this->customerFactory->create()->load($item['entity_id']);
                $item[$this->getData('name')] = $customer->getCustomerPanNumber();
            }
        }
        return $dataSource;
    }
}
?>
create customer_listing.xml file to the following path in the app/code/Magemonkeys/CustomerAttr/view/adminhtml/ui_component
<?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">
<listingToolbar name="listing_top"/>
    <columns name="customer_columns" class="MagentoCustomerUiComponentListingColumns">
        <column name="customer_pan_number" class="MagemonkeysCustomerAttrUiComponentListingColumnCustomerPancolumn">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" translate="true" xsi:type="string">Pan Number</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>
Please execute the required command
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush
Now you can check your PAN number field showing in the column and filter for the customer grid.
Fill the below form if you need any Magento relate help/advise/consulting.

With Only Agency that provides a 24/7 emergency support.

    Recent Articles
    Get a Free Quote