0

I have followed this article to create a new customer attribute and add it to the registration form: http://www.extensions.sashas.org/blog/magento-2-1-3-how-to-make-customer-attribute-update.html

This is my InstallData.php:

<?php
/**
 * @author     Sashas IT Support <support@sashas.org>
 * @copyright  2017  Sashas IT Support Inc. (http://www.extensions.sashas.org)
 * @license     http://opensource.org/licenses/GPL-3.0  GNU General Public License, version 3 (GPL-3.0)
 */


namespace Sashas\CustomerAttribute\Setup;

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

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

    /**
     * Customer setup factory
     *
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

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

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


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

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

        $setup->startSetup();

        $attributesInfo = [
            'magento_username' => [
                'label' => 'Magento Username',
                'type' => 'varchar',
                'input' => 'text',
                'position' => 1000,
                'visible' => true,
                'required' => false,
                'system' => 0,
                'user_defined' => true,
                'position' => 1000,
            ]
        ];

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

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

        foreach ($attributesInfo as $attributeCode => $attributeParams) {
            $customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
        }

        $magentoUsernameAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'magento_username');
        $magentoUsernameAttribute->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['customer_account_create'],
        ]);

        $magentoUsernameAttribute->save();

        $setup->endSetup();

    }
}

And added this to my register.phtml:

<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
  <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend>
  <div class="field my_attribute required">
    <label for="magento_username" class="label"><span><?php /* @escapeNotVerified */ echo __('Magento Username') ?></span></label>
    <div class="control">
      <input type="text" name="magento_username" id="magento_username" title="<?php /* @escapeNotVerified */ echo __('Magento Username') ?>" class="input-text" data-validate="{required:true}" autocomplete="off">
    </div>
  </div>
</fieldset>

Now, the new "Magento Username" filed appeared on the registration form. However, when checking the table customer_entity the column is missing. Or the new attribute will be located in different location?

In which table will will the new attribute be located? Also, how can I retrieve this information passed with registration form? IO would like to display the information in the account information section.

I am open for new solution too, any suggestions will be appreciated.

I am using Magento ver. 2.2.4

2

1 Answer 1

0

You can try below solution for it. It is similar to your requirement.

https://stackoverflow.com/questions/41691720/magento2-custom-attribute-is-not-saving-during-customer-registration-from-fronte

2
  • still nothing :/
    – Greg
    Nov 13, 2018 at 14:00
  • Please donot put a single link anwer
    – Amit Bera
    Nov 13, 2018 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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