3

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?

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

6 Answers 6

17

Try following way:


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

2
  • This one works for me.
    – hey
    Commented Jun 7, 2018 at 21:45
  • Subjective, but urgh assignment in condition...
    – James
    Commented Apr 6, 2020 at 10:08
5

You can try

if(null !== $customer->getCustomAttribute('my_custom_attribute'))
{
  $customer->getCustomAttribute('my_custom_attribute')->getValue();
}
2
  • This one works for me.
    – hey
    Commented Jun 7, 2018 at 21:47
  • Subjective, but urgh yoda conditions...
    – James
    Commented Apr 6, 2020 at 10:08
2

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

if($customer->getCustomAttribute('my_custom_attribute')) 
{
   $attrValue = $customer->getCustomAttribute('my_custom_attribute')->getValue();
   echo $attrValue;
} 
2
  • Doesn't this one return an AttributeInterface? I need the value...
    – hey
    Commented Jun 7, 2018 at 21:45
  • This is worked for me Commented Apr 4, 2020 at 8:18
0

You can get customer custom attribute using below code:

<?php
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;
        parent::__construct($context);
    }

    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;
    }

}
1
  • 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
    Commented Jun 7, 2018 at 21:53
0

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();
}
1
  • This seems to work, - but is the second condition ($customer->getCustomAttribute('my_custom_attribute')->getValue() !== null) really necessary to query for?
    – hey
    Commented Jun 7, 2018 at 21:50
0

I got the same error message when I tried to access a custom attribute that I have created with an UpdateData script.

The reason was that attributes are created as system attributes per default. You have to set the system option to false explicitly.

Example:

$this->customerSetup->addAttribute(Customer::ENTITY, 'blacklisted', [
    'label' => 'Customer is blacklisted',
    'type' => 'int',
    'input' => 'boolean',
    'visible' => true,
    'required' => false,
    'user_defined' => true,
    'default' => 0,
    'is_used_in_grid' => true,
    'system' => false, // <-- Notice this line!
]);

Checkout this answer for more information about the system option.

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.