protected $_customerRepositoryInterface;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->_customerRepositoryInterface = customerRepositoryInterface;
}
Now execute function here reward_barcode is custom attribute:
public function execute()
{
$customerId = 2;
if ($customerId) {
$customer = $this->_customerRepositoryInterface->getById($customerId);
$customer->setCustomAttribute(‘reward_barcode’, $barcode);
$this->_customerRepositoryInterface->save($customer);
}
$this->messageManager->addSuccess( __(‘Your membership successfully updated.’) );
}