Create the below files and add the code in your custom module
<?php MagentoFrameworkComponentComponentRegistrar::register( MagentoFrameworkComponentComponentRegistrar::MODULE, 'Magemonkeys_CustomerAttr', __DIR__ );
<?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>
<?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 [
];
}
}
<?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;
}
}
?>
<?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>

