I have created test attribute using install script it is successfully created.I have added attribute input field in register.phtml
When I register customer my custom attribute value won't save. Do I need to rewrite any model or create any plugin?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI have created test attribute using install script it is successfully created.I have added attribute input field in register.phtml
When I register customer my custom attribute value won't save. Do I need to rewrite any model or create any plugin?
I have solved it. I rewrite controller Magento\Customer\Controller\Account\CreatePost
my di.xml
code as below:
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Controller\Account\CreatePost" type="Vendor\Modulename\Controller\Account\CreatePost" />
</config>
Add below code in controller file code Vendor\Modulename\Controller\Account\CreatePost.php
$customer->setCustomAttribute('test',$this->getRequest()->getPost('test'));
You should check those fields in your install script:
'attribute_set_id' 'attribute_group_id'
They are should be set.
For example:
/** @var \Magento\Eav\Setup\EavSetupFactory $customerSetup */
$customerSetup = $this->_customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
$attributeSet = $this->_attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(Customer::ENTITY, 'phone', [
'type' => 'varchar',
'label' => 'Phone Number',
'input' => 'text',
'required' => false,
'visible' => true,
'visible_on_front' => true,
'user_defined' => true,
'sort_order' => 100,
'position' => 100,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'phone')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer', 'customer_account_edit', 'customer_account_create']
]);
$attribute->save();