1

hi i need to add new custom field in customer registration in magento 2, i was created one module

Kratos/Sellers/registration.php

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

Kratos/Sellers/etc/module.xml

<?xml version="1.0" encoding="UTF-8"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">

Kratos/Sellers/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="my_form_additional_info_customer" template="Kratos_Sellers::additionalinfocustomer.phtml"/>
    </referenceContainer>
</body>

Kratos/Sellers/view/frontend/templates/additionalinfocustomer.phtml

<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>"><div class="field required"><label for="first_name" class="label"><span><?php /* @escapeNotVerified */ echo __('Shop Name') ?></span></label><div class="control"><input type="text" name="shop_name" id="shop_name" value="" title="<?php /* @escapeNotVerified */ echo __('Shop Name') ?>" class="input-text" data-validate="{required:true}"></div></div></fieldset>

it's working show the text field in customer registration page but if i submit form the custom text field data did't store in database i was try so many time what i did mistake in my module code

1

You must be create customer attribute then it will save.

You can create customer address attribute, see below code.

<?php


namespace Kratos\Sellers\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

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

    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

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

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


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

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

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

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

        $customerSetup->addAttribute(Customer::ENTITY, 'attribute_name', [
            'type' => 'varchar',
            'label' => 'Attribute Label',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'magento_username')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => [‘adminhtml_customer_address’, ‘customer_address_edit’, ‘customer_register_address’]
        ]);

        $attribute->save();


    }
}
  • i add this file but not value store in db. Exactly what i want and what i'm doing create multi vendor module so i need add extra field in frontend customer registration form this process is don using above code but the value not store in db. If this value store in db which table having this value because this value i add to admin panel customer listing – Rajesh Nagappan Apr 28 '17 at 9:23

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.