0

Trying to update custom customer attribute value while customer edit from frontend , and my field is file upload control but unable to save the attribute

Tried

$customer = $observer->getEvent()->getCustomer();
$customer->setCustomAttribute('profileimage', '123.jpg');
$this->customerRepository->save($customer);

but getting error Call to a member function setCustomAttribute() on null

secondly, I have also try

$event = $observer->getEvent();
$customer = $observer->getCustomerDataObject();
$customerId = $customer->getId();
$customerData = $this->_customerRepositoryInterface->getById($customerId);
$customerData->setCustomAttribute('profileimage', '123.jpg');
$customer->updateData($customerData);
$customerResource = $this->customerFactory->create();
$customerResource->saveAttribute($customer, 'profileimage');

Still getting error for updateData() function not found.

can anyone help me out to save custom customer attribute from frontend for image in Magento 2.

1
  • How you create the attribute? Share that code. Commented Jan 4, 2021 at 11:01

1 Answer 1

0

You can try something like below.

protected $customer;

protected $customerFactory;

public function __construct(
    \Magento\Customer\Model\Customer $customer
    \Magento\Customer\Model\ResourceModel\CustomerFactory $customerFactory
)
{
    $this->customer = $customer;
    $this->customerFactory = $customerFactory;
}

$customerId = "1";
$customer = $this->customer->load($customerId);
$data = "customer attribute value";
$customerData = $customer->getDataModel();
$customerData->setCustomAttribute('customer_attribute_code',$data);
$customer->updateData($customerData);
$customerResource = $this->customerFactory->create();
$customerResource->saveAttribute($customer, 'customer_attribute_code');

It will work fine. Please check and let me know still you got the error.

Also, you can download extension which is already developed.

Thanks

3
  • Tried above code but unfortunately it doesn't work for me Commented Jan 5, 2021 at 4:17
  • Tried above code but unfortunately it doesn't work for me , the custom attribute which i have created is of type 'file' and whatever solutions i have got works for input type 'text' not for 'file'. my custom field is $customerSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, 'profileimage', [ 'type' => 'text', 'label' => 'Profile Picture', 'input' => 'file', not working for this. Commented Jan 5, 2021 at 4:24
  • while trying to update attribute of type 'file' i'm getting below error Fatal error: Uncaught Magento\Framework\Exception\FileSystemException: Cannot gather stats! Warning!stat(): stat failed for C:/xampp741/htdocs/mag240s/pub/media/customer/123.jpg in C:\xampp741\htdocs\mag240s\vendor\magento\framework\Filesystem\Driver\File.php:77 Commented Jan 5, 2021 at 4:27

Your Answer

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

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