Please see this screenshot,
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();
}
}**
-
Check this reference link Create Custom Field in User Registration PageRenga– Renga2018年04月05日 08:03:26 +00:00Commented Apr 5, 2018 at 8:03
2 Answers 2
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
Add additional field in a customer account.
Check the reference link Reference Link
Explore related questions
See similar questions with these tags.