1

I have created an attribute ZipCode just for the registration of a new customer.

In eav_attribute table it exists and it's created correctly. However I have not managed to put the attribute in the new account form fields.

Here is my code:

InstallData.php

<?php
namespace Kage\RegisterForm\Setup;

use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface 
{
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * @var \Magento\Eav\Model\Config
     */
    private $eavConfig;

    public function __construct(
        EavSetupFactory $eavSetupFactory,
        \Magento\Eav\Model\Config $eavConfig
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
    }

    /**
     * Installs data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var \Magento\Eav\Setup\EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $setup->startSetup();

        $attributeCode = 'register_zip_code';
        $eavSetup->addAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'register_zip_code', [
            'type' => 'text',
            'label' => 'Zip Code',
            'input' => 'text',
            'required' => true,
            'visible' => true,
            'source' => '',
            'backend' => '',
            'user_defined' => true,
            'is_user_defined' => false,
            'sort_order' => 1000,
            'is_used_in_grid' => false,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => false,
            'is_searchable_in_grid' => false,
            'position' => 1000,
            'default' => 0,
            'system' => 0,
        ]);

        $eavSetup->addAttributeToSet(
            CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
            CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
            null,
            $attributeCode);

        $registerZipCode= $this->eavConfig->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode);
        $registerZipCode->setData('used_in_forms', [
            'adminhtml_customer',
            'customer_account_create',
            'customer_account_edit'
        ]);
        $registerZipCode->getResource()->save($registerZipCode);

        $setup->endSetup();
    }
}

customer_create_account.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="customer_form_login">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Kage_RegisterForm::form\register.phtml</argument>
            </action>
        </referenceBlock>
</body>
</page>

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>
    <p>
    <div class="field required regulation">
        <label for="regulation" class="label"><span><?php /* @escapeNotVerified */
                echo __('Zip Code') ?></span></label>
        <div class="control">
            <input type="text" name="register_zip_code" id="register_zip_code" title="<?php /* @escapeNotVerified */ echo __('Zip Code') ?>" class="input-text" data-validate="{required:true}">
        </div>
    </div>
    </p>
</fieldset>

The closes thing I achieved, was that the create new account form was blank, I don't want to replace fields there, I just wan't to add this extra field there.

Thanks!

2

If your install data script successfully installed your custom attribute, now you just need to override addition information .phtml file and set your custom attribute in that.

Create Vendor/TestModule/view/frontend/layout/customer_account_create.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="form.additional.info">
        <block class="Magento\Framework\View\Element\Template" name="additional_zipcode" template="Kage_RegisterForm::form\register.phtml"/>
    </referenceContainer>
</body>
</page>

Create Vendor/TestModule/view/frontend/templates/form/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>
    <p>
    <div class="field regulation">
        <label for="regulation" class="label"><span><?php /* @escapeNotVerified */
                echo __('Additional Zipcode') ?></span></label>
        <div class="control">
            <input type="text" name="additional_zipcode" id="additional_zipcode" title="<?php /* @escapeNotVerified */ echo __('Additional Zipcode') ?>" class="input-text" data-validate="{required:false}">
        </div>
    </div>
    </p>
</fieldset>

THANKS.

3
  • Hi, thanks, but it's still not showing the extra field, I'm searching for the field when click create new account but see no extra field – Oscar Vazquez Jul 28 '20 at 14:20
  • After change you run setup:upgrade command – Mohit Patel Jul 28 '20 at 15:52
  • Thanks it works. I found that register.phtml inside form folder did not work, so I put it out from that folder and now it works. – Oscar Vazquez Jul 28 '20 at 16:52
1

The layout file will be customer_account_create.xml not customer_create_account.xml

And please change the content as below

From:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="customer_form_login">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Kage_RegisterForm::form\register.phtml</argument>
            </action>
        </referenceBlock>
</body>
</page>

To:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="form.additional.info">
                <block class="Magento\Framework\View\Element\Template" name="register_zip_code" template="Kage_RegisterForm::form\register.phtml""/>
        </referenceContainer>
</body>
</page>
1
  • I have modified it, but still cannot see the new field :( – Oscar Vazquez Jul 28 '20 at 14:20

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.