How to Show Customer Attribute in Magento 2

attributescustomermagento2module

I created a new module to add a extra customer attribute.. but it only appear in admin site(customers/all customers/edit/Account information). But i want it into customer registration form… how i can do it ?
im using: frontend_customer and adminhtml_customer

Socialme/IdInput/Setup/InstallData.php

<?php

namespace Socialme\IdInput\Setup;


use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;



/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{

    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }


    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

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

        $customerSetup->addAttribute(Customer::ENTITY, 'id_number', [
            'type' => 'varchar',
            'label' => 'ID Number',
            'input' => 'text',
            'required' => true,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'id_number')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['frontend_customer','adminhtml_customer'],
        ]);

        $attribute->save();


    }
}

Socialme/IdInput/view/frontend/templates/customer/form/register.phtml

<div class="field required">
            <label for="id_number" class="label"><span><?php echo __('ID Number')?></span></label>
            <input type="text" name="id_number" id="id_number" title="id_number" class="input-text"  value="<?php echo $block->escapeHtml($block->getFormData()->getIdNumber()) ?>" required">
        </div>

Best Answer

You can try with used_in_forms value,

customer_account_create

You have to change in your php file with below value,

'used_in_forms' => ['customer_account_create','adminhtml_checkout','adminhtml_customer'],

Thanks.

Related Topic