SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH

how to add attribute customer register magento 2 ?

This is how to add an information field in customer page in magento 2 . we need to add a field in magento’s data table by add the file InstallData.php in folder Setup in module .

<?php
namespace Magepow\Customer\Setup;

use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
    private $eavSetupFactory;
    private $eavConfig;
    private $attributeResource;

    public function __construct(
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        \Magento\Eav\Model\Config $eavConfig,
        \Magento\Customer\Model\ResourceModel\Attribute $attributeResource
    ){
        $this->eavSetupFactory = $eavSetupFactory;     
        $this->eavConfig = $eavConfig;
        $this->attributeResource = $attributeResource; 
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->removeAttribute(Customer::ENTITY, "new_customer_custom");

        $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
        $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);

        $eavSetup->addAttribute(Customer::ENTITY, 'new_customer_custom', [

            'type' => 'varchar',
            'label' => 'New Custom',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 990,
            'position' => 990,
            'system' => 0,
        ]);

     $attribute =$this>eavConfig>getAttribute(Customer::ENTITY,'new_customer_custom');
        $attribute->setData('attribute_set_id', $attributeSetId);
        $attribute->setData('attribute_group_id', $attributeGroupId);


        $attribute->setData('used_in_forms', [
            'customer_account_create',
            'customer_account_edit',
            'adminhtml_customer'
        ]);
        $this->attributeResource->save($attribute);
    }

}

Note: about the part used “used_in_forms” by calling you it may not show up in whatever case you want.

then create a file field_custom.phtml on module in Module\NameModule\view\templates

<field class="field new_customer_custom">
    <label class="label" for="new_customer_custom">
        <span>new Custom</span>
    </label>     
    <div class="control">
        <input type="hidden" name="new_customer_custom" id="new_customer_custom" value="" title="<?= $block->escapeHtmlAttr(__('New Custom')) ?>" class="input-text" data-validate="{required:false}">
    </div>
</field>

here we just need to add a field in customer create so we will create an invite to add a block in the layout create customer customer_account_create.xml

<?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="form.additional.info">
         <block class="Magento\Framework\View\Element\Template" name="form_additional_info_customer" template="field_custom.phtml"/>
     </referenceContainer>
 </body>
</page>

this is how to call the added attribute in customer

use Magento\Customer\Api\CustomerRepositoryInterface;
.....................
public function __construct(
        CustomerRepositoryInterface $customerRepository
    ) {

        $this->customerRepository = $customerRepository;

}
.................
public function getCustomer($customerId){
 $customerData = $this->customerRepository->getById($customerId);
if($customerData->getCustomAttribute('new_customer_custom') != null){
$value = $customerData->getCustomAttribute('new_customer_custom')->getValue();
   return $value;         
}

}