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();
    }
}**
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

1

Add additional field in a customer account.

Check the reference link Reference Link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.