4

I want to add mobile no field in registration form. here is the code

InstallData.php

<?php
/**
 * Copyright © 2016 Rouven Alexander Rieker
 * See LICENSE.md bundled with this module for license details.
 */
namespace Semaio\AdvancedLogin\Setup;

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

/**
 * Class InstallData
 *
 * @package Semaio\AdvancedLogin\Setup
 */
class InstallData implements InstallDataInterface
{
    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface   $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

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

        // Add new customer attribute
        $customerSetup->addAttribute(
            Customer::ENTITY,
            'customer_number',
            [

                'label'                 => 'Customer Number',
                'input'                 => 'text',
                'required'              => false,
                'sort_order'            => 10,
                'visible'               => true,
                'system'                => false,
                'is_used_in_grid'       => true,
                'is_visible_in_grid'    => true,
                'is_filterable_in_grid' => true,
                'is_searchable_in_grid' => true
            ]
        );


        // add attribute to form
        /** @var  $attribute */
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'customer_number');
        $attribute->setData('used_in_forms', ['adminhtml_customer','checkout_register','customer_account_create','customer_account_edit','adminhtml_checkout']);
        $attribute->save();

        $setup->endSetup();
    }
}

view/frontend/templates/form/register.phtml

<div class="field required">
    <label for="customer_number" class="label"><span><?= __('CustomerNumber') ?></span></label>
    <div class="control">
        <input type="text" name="customer_number" id="customer_number" value="<?= $block->escapeHtml($block->getFormData()->getCustomerNumber()) ?>" title="<?= __('CustomerNumber') ?>" class="input-text" data-validate="{required:true, 'validate-phoneStrict':true}">
    </div>
</div>

can anyone help me where is the problem?

  • Did you check the customer_number attribute in eav_attribute table? – Dinesh Yadav Sep 11 '18 at 10:01
  • yes..it is there. @DineshYadav – Bhakti Thakkar Sep 11 '18 at 10:01
  • What is the case? 1. Text box is not appearing on frontend. 2. Text box is appearing but value is not being saved in the database. If it is case 1 then simply try to add an input box in your form. First try to show the text box then go for the customer attribute. – Dinesh Yadav Sep 11 '18 at 10:15
  • text box is appearing but value is not being saved in the database. – Bhakti Thakkar Sep 11 '18 at 12:45
  • I have added my answer. Please remove your current attribute and recreate using the script given in the answer. After that, try to save the value from admin panel in any customer so you will get an idea if your script is working or not. – Dinesh Yadav Sep 12 '18 at 5:47
2

app/design/frontend/{Vendor}/{theme}/Magento_Customer/templates/form/register.phtml

<div class="field required">
    <label for="customer_number" class="label"><span><?= __('CustomerNumber') ?></span></label>
    <div class="control">
        <input type="text" name="customer_number" id="customer_number" value="<?= $block->escapeHtml($block->getFormData()->getCustomerNumber()) ?>" title="<?= __('CustomerNumber') ?>" class="input-text" data-validate="{required:true, 'validate-phoneStrict':true}">
    </div>
</div>
1

Assign customer group id and attribute set id in your script in order to save the value in your database.

Remove your attribute and recreate it using following script.

<?php
/**
 * Copyright © 2016 Rouven Alexander Rieker
 * See LICENSE.md bundled with this module for license details.
 */
namespace Semaio\AdvancedLogin\Setup;

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

/**
 * Class InstallData
 *
 * @package Semaio\AdvancedLogin\Setup
 */
class InstallData implements InstallDataInterface
{
    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;

    private $attributeSetFactory;

    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory,
        \Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }

    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface   $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

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

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        $attributeGroupId = $this->attributeSetFactory->create()->getDefaultGroupId($attributeSetId);

        // Add new customer attribute
        $customerSetup->addAttribute(
            Customer::ENTITY,
            'customer_number',
            [
                'type' => 'varchar',
                'label' => 'Customer Number',
                'input' => 'text',
                'required' => false,
                'sort_order' => 10,
                'visible' => true,
                'system' => false,
                'is_used_in_grid' => true,
                'is_visible_in_grid' => true,
                'is_filterable_in_grid' => true,
                'is_searchable_in_grid' => true
            ]
        );

        // add attribute to form
        /** @var  $attribute */
        $usedinform[] = "adminhtml_customer";
        $usedinform[] = "checkout_register";
        $usedinform[] = "customer_account_create";
        $usedinform[] = "customer_account_edit";
        $usedinform[] = "adminhtml_checkout";

        $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "customer_number")
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'sort_order' => 1000,
            'used_in_forms' => $usedinform,
        ]);

        $attribute->save();

        $setup->endSetup();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.