We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    For example, when we create a mobile number customer attribute in Magento 2,  we need to create a module to add new customer attribute in Magento2.

    Create a registration.php file to the following path
    app/code/Magemonkeys/CustomerAttribute/registration.php

    <?php
    MagentoFrameworkComponentComponentRegistrar::register(
        MagentoFrameworkComponentComponentRegistrar::MODULE,
        'Magemonkeys_CustomerAttribute',
        __DIR__
    );

    Create module.xml file to the following path  app/code/Magemonkeys/CustomerAttribute/etc/module.xml which define our module setup version to setup_module table.

    <?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_CustomerAttribute" setup_version="1.0.0">
        <sequence>
          <module name="Magento_Customer"/>
        </sequence>
      </module>
    </config>

    Then, we need to create an extension_attributes.xml file to the following path app/code/Magemonkeys/CustomerAttribute/etc/extension_attributes.xml

    which defines our mobile number customer attribute.

    Pass Classname in extension_attributes.xml as MagentoCustomerApiDataCustomerInterface because its a base Customer interface to create customer attributes in Magento2.

    Set attribute code as your customer attribute code and define type as a string.

    <?xml version="1.0" ?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
        <extension_attributes for="MagentoCustomerApiDataCustomerInterface">
            <attribute code="mobile" type="string"/>
        </extension_attributes>
    </config>

    For Create Customer attribute we need to define InstallData.php file to the following path app/code/Magemonkeys/CustomerAttribute/Setup/InstallData.php which adds an entry to our database for custom mobile number attribute.

    <?php
    
    namespace MagemonkeysCustomerAttributeSetup;
    
    use MagentoFrameworkSetupInstallDataInterface;
    use MagentoFrameworkSetupModuleContextInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoCustomerModelCustomer;
    use MagentoCustomerSetupCustomerSetupFactory;
    
    class InstallData implements InstallDataInterface
    {
    
        private $customerSetupFactory;
    
        /**
         * Constructor
         *
         * @param MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory
         */
        public function __construct(
            CustomerSetupFactory $customerSetupFactory
        ) {
            $this->customerSetupFactory = $customerSetupFactory;
        }
    
        /**
         * {@inheritdoc}
         */
        public function install(
            ModuleDataSetupInterface $setup,
            ModuleContextInterface $context
        ) {
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    
            $customerSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY, 'mobile', [
                'type' => 'varchar', // type of attribute
                'label' => 'Mobile Number',
                'input' => 'text', // input type
                'source' => '',
                'required' => false, // if you want to required need to set true
                'visible' => true,
                'position' => 500, // position of attribute
                'system' => false,
                'backend' => ''
            ]);
            
            /* Specify which place you want to display customer attribute */
            $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'mobile')
            ->addData(['used_in_forms' => [
                    'adminhtml_customer',
                    'adminhtml_checkout',
                    'customer_account_create',
                    'customer_account_edit'
                ]
            ]);
            $attribute->save();
        }
    }

    Now Run the upgrade command to install our module using SSh from the root of your Magento instance.

    php bin/magento setup:upgrade
    php bin/magento setup:static-content:deploy -f
    php bin/magento indexer:reindex
    php bin/magento cache:flush

    Now you can check your new mobile number attribute at the admin panel.

    Go To, Customers -> All Customers -> Edit any Customer.

    field_5bfb909c5ccae

      Get a Free Quote