Magento 2: Добавьте настраиваемое поле в форму регистрации клиента без создания атрибута клиента


Я хочу добавить пользовательское поле в форму регистрации клиента в magento2.

Я не хочу делать его атрибутом клиента и хочу сохранить в пользовательской таблице.

Кто-нибудь может мне помочь

Author: Ankita Patel, 2019-11-18

1 answers

Создайте модуль с именем Vendor_Module. Создайте все файлы и структуру, как показано ниже:

Шаг 1:

App/code/Vendor/Module/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

Шаг 2:

App/code/Vendor/Module/etc/module.xml

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

Шаг 3:

App/code/Vendor/Module/view/frontend/layout/customer_account_create.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="form.additional.info">
            <block class="Magento\Customer\Block\Form\Register" name="additional_info_customer_custom_fields" template="Vendor_Module::customer/customfields.phtml" before="-"/>
        </referenceContainer>
    </body>
</page>

Шаг 4:

Приложение/код/Поставщик/Модуль/представление/интерфейс/шаблоны/клиент/пользовательские поля.phtml

<div class="field custom required">
    <label for="custom" class="label"><span><?= $block->escapeHtml(__('Custom')) ?></span></label>
    <div class="control">
        <input name="custom" type="text" value="" class="input-text" autocomplete="false" title="Custom" data-validate="{required:true}" />
    </div>
</div>

Шаг 5:

App/code/Vendor/Module/etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_register_success">
        <observer name="custom_register" instance="Vendor\Module\Observer\Accountcreate" shared="false" />
    </event>
</config>

Шаг 6:

App/code/Vendor/Module/Observer/Accountcreate.php

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class Accountcreate implements ObserverInterface
{
    protected $request;

    public function __construct(
        ... Inject Your custom model where your want to save the data
        \Magento\Framework\App\Request\Http $request,
        ...
    ) {
        ...
        $this->request = $request;
        ...
    }

    public function execute(Observer $observer)
    {
        $custom = $this->request->getParam('custom');
        Now load your model and save the data, you will get the custom data in $custom field.
    }   
}

Выполните необходимые команды и протестируйте.

 3
Author: Sukumar Gorai, 2019-11-18 14:00:12