How to create custom customer attribute in magento 2 - Magesan

How to create custom customer attribute in magento 2

How to create custom customer attribute in magento 2

Custom attributes are somewhat we need to create in our day to day life for adding additional functionality.
Customer attributes are limited and if we need additional functions then we need to create custom customer attribute.
Here, we create phone_number field using UpgradeData.php

app/code/Magesan/Extension/Setup/UpgradeData.php

<?php

namespace Magesan\Extension\Setup;

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

class UpgradeData implements UpgradeDataInterface
{
    const PHONE_NUMBER = 'phone_number';

    protected $customerSetupFactory;

    private $attributeSetFactory;

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

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

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

        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        if (version_compare($context->getVersion(), '1.0.0') < 0) {
            $customerSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                self::PHONE_NUMBER,
                [
                    'type'         => 'text',
                    'label'        => 'Phone Number',
                    'input'        => 'text',
                    'required'     => true,
                    'visible'      => true,
                    'user_defined' => true,
                    'sort_order'   => 1000,
                    'position'     => 1000,
                    'system'       => 0,
                ]
            );
            $Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, self::PHONE_NUMBER)
                ->addData([
                    'attribute_set_id'   => 1,
                    'attribute_group_id' => 1,
                    'used_in_forms'      => ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout'],
                ]);

            $Attribute->save();
        }

        $setup->endSetup();
    }
}

Save the file and fire command
php bin/magento setup:upgrade
php bin/magento cache:clean
After creating attribute. We need to call that attribute in required file, Like on customer registration file we need to add some code of block in below file.

app/design/frontend/Magesan/theme/Magento_Customer/templates/form/register.phtml

<div class="field">
     <label for="phone_number" class="label"><span><?= $escaper->escapeHtml(__('MOBILE')) ?></span></label>
     <div class="control">
          <input type="text" name="phone_number" id="phone_number" value="<?= $escaper->escapeHtmlAttr($formData->getPhoneNumber()) ?>" title="<?= $escaper->escapeHtmlAttr(__('phone_number')) ?>" class="input-text" data-mage-init='{"mage/trim-input":{}}' />
     </div>
</div>

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>