How to prevent “Call to a member function getValue() on null” when using getCustomAttribute()->getValue()?
-
13-04-2021 - |
Question
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?
Solution
Try following way:
if($myCustomAttribute = $customer->getCustomAttribute('my_custom_attribute')) {
echo $myCustomAttribute->getValue();
}
OTHER TIPS
You can try
if(null !== $customer->getCustomAttribute('my_custom_attribute'))
{
$customer->getCustomAttribute('my_custom_attribute')->getValue();
}
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;
}
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;
}
}
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();
}
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.