Custom/CustomerAttribute/Setup/Patch/Data/AddPhoneAttribute.php
<?php
namespace CustomCustomerAttributeSetupPatchData;
use MagentoCatalogUiDataProviderProductProductCollectionFactory;
use MagentoCustomerModelCustomer;
use MagentoEavModelConfig;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoFrameworkSetupPatchPatchRevertableInterface;
use PsrLogLoggerInterface;
/**
* Class AddPhoneAttribute
* @package MagenestCustomerAttributeSetupPatchData
*/
class AddPhoneAttribute implements DataPatchInterface, PatchRevertableInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* @var ProductCollectionFactory
*/
private $productCollectionFactory;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var Config
*/
private $eavConfig;
/**
* @var MagentoCustomerModelResourceModelAttribute
*/
private $attributeResource;
/**
* AddPhoneAttribute constructor.
* @param EavSetupFactory $eavSetupFactory
* @param Config $eavConfig
* @param LoggerInterface $logger
* @param MagentoCustomerModelResourceModelAttribute $attributeResource
*/
public function __construct(
EavSetupFactory $eavSetupFactory,
Config $eavConfig,
LoggerInterface $logger,
MagentoCustomerModelResourceModelAttribute $attributeResource,
MagentoFrameworkSetupModuleDataSetupInterface $moduleDataSetup
) {
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
$this->logger = $logger;
$this->attributeResource = $attributeResource;
$this->moduleDataSetup = $moduleDataSetup;
}
/**
* {@inheritdoc}
*/
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();
$this->addPhoneAttribute();
$this->moduleDataSetup->getConnection()->endSetup();
}
/**
* @throws MagentoFrameworkExceptionAlreadyExistsException
* @throws MagentoFrameworkExceptionLocalizedException
* @throws Zend_Validate_Exception
*/
public function addPhoneAttribute()
{
$eavSetup = $this->eavSetupFactory->create();
$eavSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'phone_number',
[
'type' => 'varchar',
'label' => 'Phone Number',
'input' => 'text',
'value' => '',
'required' => 1,
'visible' => 1,
'user_defined' => 1,
'sort_order' => 999,
'position' => 999,
'system' => 0
]
);
$attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
$attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);
$attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'phone_number');
$attribute->setData('attribute_set_id', $attributeSetId);
$attribute->setData('attribute_group_id', $attributeGroupId);
$attribute->setData('used_in_forms', [
'adminhtml_customer',
]);
$this->attributeResource->save($attribute);
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
/**
*
*/
public function revert()
{
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
}
Custom/CustomerAttribute/view/frontend/layout/customer_account_create.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="MagentoFrameworkViewElementTemplate"
name="phone_number"
template="Custom_CustomerAttribute::extra_field.phtml"/>
</referenceContainer>
</body>
</page>
app/code/Custom/CustomerAttribute/view/frontend/templates/extra_field.phtml
<div class="field phone_number required">
<label class="label" for="phone_number">
<span><?= $block->escapeHtml(__('Phone number')) ?></span>
</label>
<div class="control">
<input type="text" name="phone_number" id="phone_number" title="<?= $block->escapeHtmlAttr(__('Phone number')) ?>" class="input-text" data-validate="{required:true}">
</div>
</div>
I’m using Magento 2.4,6
I used the above code to add the phone number to the registration page. phone number is showing but getting the error "Phone Number" is a required value.
while creating an account.
I request that everyone please guide me on this issue. Thank you in advance.