0

How to get the value of customer custom attribute load by customer Id in phtml file in Magento2.3.0?

$customer = $this->customerRepositoryInterface->getById($customerId); 
$value = $customer->getCustomAttribute('business type)->getValue(); 

I have referred many links, but not able to achieve the value. Any help would be appreciated.

  • Can you share what you tried so that I can correct it and make it work. – anonymous Jul 23 '19 at 12:54
  • $customer = $this->customerRepositoryInterface->getById($customerId); $value = $customer->getCustomAttribute('business type)->getValue(); and I have also tried with objectmanager. But not able to achieve the value. – Amy Jul 23 '19 at 12:59
  • amy should by use attribute code in getCustomAttribute() method. Don't be use attribute label – Arunprabakaran M Jul 23 '19 at 13:05
0

Try this,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$session = $objectManager->get('Magento\Customer\Model\Session');    

/* get Customer id */
$customerId=$session->getCustomer()->getId();
$customerEmail=$session->getCustomer()->getEmail();

/* load customer collection by Customer id */
$customerRepository = $objectManager
        ->get('Magento\Customer\Api\CustomerRepositoryInterface');
$customer = $customerRepository->getById($customerId);

/*get customer custom attribute value by customer attribute code */
$cattrValue = $customer->getCustomAttribute('custom_attribute')->getValue();

Note:Code tested

| improve this answer | |
  • I hope you got answer – Arunprabakaran M Jul 23 '19 at 13:20
  • Nope, Page showing empty.. – Amy Jul 23 '19 at 13:43
  • which phtml file are you working? – Arunprabakaran M Jul 23 '19 at 14:05
  • It is my custom phtml file. – Amy Jul 23 '19 at 14:08
  • try with logged in user – Arunprabakaran M Jul 23 '19 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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