Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top