Magento2 – Custom Customer Attribute Value Not Saving During Registration

customer-attributemagento2

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?

Best Answer

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'));
Related Topic