Set value to created customer custom attribute in magento 2
Check below code is working great for me. Tested with Magento 2.3.3
$avatar = $this->request->getParam('avatar');
// \Magento\Customer\Model\Customer $customerModel,
$customerNew = $this->customerModel->load($this->customerSession->getCustomer()->getId());
$customerData = $customerNew->getDataModel();
$customerData->setCustomAttribute('customer_avatar',$customerAvatar);
$customerNew->updateData($customerData);
// \Magento\Customer\Model\ResourceModel\CustomerFactory $customerFactory
$customerResource = $this->customerResourceFactory->create();
$customerResource->saveAttribute($customerNew, 'customer_avatar');
This may help for you to save the custom attribute.
protected $_customerRepositoryInterface;
public function __construct(
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
\Magento\Customer\Model\CustomerFactory $customerFactory
) {
$this->_customerRepositoryInterface = $customerRepositoryInterface;
$this->_customerFactory = $customerFactory;
}
public function execute()
{
$customerId = 2; // Corresponding customer id
$customer = $this->_customerFactory->create()->load($customerId)->getDataModel();
$customer->setCustomAttribute('custom_attribute_name', 'value');
$this->_customerRepositoryInterface->save($customer);
}
To save custom customer attribute
Code Tested in Magento v2.2.1 and it working.
namespace Vendor\Module\Controller\Index;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\ResourceModel\CustomerFactory as CustomerResourceFactory;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
/**
* Class Index
*/
class Index extends Action
{
const CUSTOM_CUSTOMER_ATTR = 'xyz';
/**
* @var CustomerResourceFactory
*/
protected $customerResourceFactory;
/**
* @var Customer
*/
protected $customerModel;
/**
* @var CustomerSession
*/
protected $customerSession;
/**
* Index constructor.
* @param Context $context
* @param CustomerResourceFactory $customerResourceFactory
* @param Customer $customerModel
* @param CustomerSession $customerSession
*/
public function __construct(
Context $context,
CustomerResourceFactory $customerResourceFactory,
Customer $customerModel,
CustomerSession $customerSession
)
{
parent::__construct($context);
$this->customerResourceFactory = $customerResourceFactory;
$this->customerModel = $customerModel;
$this->customerSession = $customerSession;
}
/**
* @return \Magento\Framework\App\ResponseInterface
*/
public function execute()
{
/* ********* Start : Important area : to save custom customer attribute value without losing session on refresh *********** */
if ($this->customerSession->isLoggedIn()) {
$customAttributeValue = $this->getRequest()->getParam('custom_attribute'); // it just an example to pull value
$customerId = $this->customerSession->getCustomer()->getId();
$customerNew = $this->customerModel->load($customerId);
$customerData = $customerNew->getDataModel();
$customerData->setCustomAttribute(self::CUSTOM_CUSTOMER_ATTR, $customAttributeValue);
$customerNew->updateData($customerData);
$customerResource = $this->customerResourceFactory->create();
$customerResource->saveAttribute($customerNew, self::CUSTOM_CUSTOMER_ATTR);
/* ********* End: Important area : to save custom customer attribute value without losing session on refresh *********** */
$this->messageManager->addSuccess(__('You are successfully save the data!!'));
}
return $this->_redirect('route/controller/action'); // it just an example for return
}
}