1

Please see this screenshot,

enter image description here

I try with Install Data

<?php
namespace CP\Customerattribute\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;
/**
* Install data
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
 /**
 * CustomerSetupFactory
 * @var CustomerSetupFactory
 */
 protected $customerSetupFactory;
 /**
 * $attributeSetFactory
 * @var AttributeSetFactory
 */
 private $attributeSetFactory;
 /**
 * initiate object
 * @param CustomerSetupFactory $customerSetupFactory
 * @param AttributeSetFactory $attributeSetFactory
 */
 public function __construct(
 CustomerSetupFactory $customerSetupFactory,
 AttributeSetFactory $attributeSetFactory
 ) {
 $this->customerSetupFactory = $customerSetupFactory;
 $this->attributeSetFactory = $attributeSetFactory;
 }
 /**
 * install data method
 * @param ModuleDataSetupInterface $setup
 * @param ModuleContextInterface $context
 */
 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);
 /**
 * customer registration form default field mobile number
 */
 $customerSetup->addAttribute(Customer::ENTITY, 'custom_field', [
 'type' => 'varchar',
 'label' => 'ID Documents',
 'input' => 'text',
 'required' => true,
 'visible' => true,
 'user_defined' => true,
 'sort_order' => 1000,
 'position' => 1000,
 'system' => 0,
 ]);
 //add attribute to attribute set
 $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_field')->addData([
 'attribute_set_id' => $attributeSetId,
 'attribute_group_id' => $attributeGroupId,
 'used_in_forms' => ['adminhtml_customer', 'customer_account_create'],
 ]);
 $attribute->save();
 }
}**
Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Apr 5, 2018 at 6:03
1

2 Answers 2

1

You can override this controller Magento\Customer\Controller\Account\CreatePost with di.xml

and Below code will help you to save custom attribute value

 <?php
 public function __construct(
 \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
 \Magento\Framework\App\Action\Context $context
 )
 { 
 $this->customerRepository = $customerRepository;
 parent::__construct($context);
 }
 public function execute(){
 $customerId = XX;
 $customer = $this->customerRepository->getById($customerId);
 $customer->setCustomAttribute('custom_attribute!','value for attribute');
 try { 
 $customer = $this->customerRepository->save($customer);
 }catch (Exception $e) {
 return $e->getMessage();
 }
 }

So what we did here, in Magento 2 we have to user setCustomAttribute method to save custom customer attribute.

For overriding that controller you may refer to this post https://magento.stackexchange.com/a/154517/49821

answered Apr 5, 2018 at 6:23
1

Add additional field in a customer account.

Check the reference link Reference Link

answered Apr 5, 2018 at 8:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.