My colleague wrote some code, which is hidden behind an interface:

$myCustomValue = $customer->getCustomAttribute('my_custom_attribute')->getValue();

Working on integrations now, I invoke his code via an API and just came across customers who do not have a value set for that attribute.

The error message I get is:

Call to a member function getValue() on null

How can I modify his code best, so that it checks first if the value is set for the specific customer?

  • Please use : $myCustomValue = $customer->getCustomAttribute('my_custom_attribute'); – Dhaduk Mitesh Jun 7 '18 at 4:56
  • Also try this : $customer->getData('my_custom_attribute'); – Dhaduk Mitesh Jun 7 '18 at 4:59
  • Check my answer will help you to get value. – Nikunj Vadariya Jun 7 '18 at 5:31
  • Check that attribute exist. – kunj Jun 7 '18 at 5:51

Try following way:

if($myCustomAttribute = $customer->getCustomAttribute('my_custom_attribute')) {
    echo $myCustomAttribute->getValue();

|improve this answer|||||
  • This one works for me. – hey Jun 7 '18 at 21:45

You can try

if(null !== $customer->getCustomAttribute('my_custom_attribute'))
|improve this answer|||||
  • This one works for me. – hey Jun 7 '18 at 21:47

To get the customer attribute, you can use like this:

   $attrValue = $customer->getCustomAttribute('my_custom_attribute')->getValue();
   echo $attrValue;
|improve this answer|||||
  • Doesn't this one return an AttributeInterface? I need the value... – hey Jun 7 '18 at 21:45
  • This is worked for me – shivashankar m yesterday

You can get customer custom attribute using below code:

namespace Amitshree\Marketplace\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
    protected $resultPageFactory;

    public function __construct(        
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
    ) {  
        $this->resultPageFactory = $resultPageFactory;
        $this->customerRepositoryInterface = $customerRepositoryInterface;

    public function execute()
        $customerId = 5;
        $customer = $this->customerRepositoryInterface->getById($customerId);
        $customerAttributeData = $customer->__toArray();
        $isVendor = $customerAttributeData['custom_attributes']['is_vendor']['value'];
        echo "<pre>";print_r($customerAttributeData['custom_attributes']);exit;

|improve this answer|||||
  • Oh, a nice alternative approach. I think in this case we should add some issets: if (isset($customerAttributeData['custom_attributes'])) && isset($customerAttributeData['custom_attributes']['is_vendor']) ...otherwise we might run into the same problem with the undefined indexes (depending on php settings). – hey Jun 7 '18 at 21:53

This happens with products as well, if you create a custom attribute and don't set values against this attribute for products, then getting these attribute against such products will return null.

To avoid running into error, always make a check before calling a custom attribute to make sure if its value is set or not.

e.g Something like this should work.

if($customer->getCustomAttribute('my_custom_attribute') !== null && $customer->getCustomAttribute('my_custom_attribute')->getValue() !== null) {
    echo $customer->getCustomAttribute('my_custom_attribute')->getValue();
|improve this answer|||||
  • This seems to work, - but is the second condition ($customer->getCustomAttribute('my_custom_attribute')->getValue() !== null) really necessary to query for? – hey Jun 7 '18 at 21:50

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.