0

This question already has an answer here:

I have added few custom attributes and one of them is is_mobile_verified while registering I dont set any value for this however, while creating the attribute I set default value as 0.

now after registration, I am trying to get the value to do some other functionalities. I am facing issues at this point.

        $customer_id = 39; // SETTING SOME ID FOR TESTING PURPOSE
        $is_verified = $customer->getCustomAttribute('is_mobile_verified')->getValue();

If I get the value using this code I am getting error message as below

Fatal error: Uncaught Error: Call to a member function getValue() on null in C:\xampp\htdocs\

Because there is no value updated to this fields so for. Once from the admin section if I updated as 0 Then it returns the value.

Is there a way to restrict this behavior as in the earlier scenario also, if there is no value exist I can get empty value. Apparently this is what the actual behavior in Magento1.x

marked as duplicate by Rohan Hapani, Manashvi Birla, Murtuza Zabuawala, Mohit Kumar Arora, Aditya Shah Dec 27 '18 at 13:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Can you update the full code as your code shows you have not loaded customer? – Sukumar Gorai Dec 27 '18 at 8:41
1

If the custom attribute of the customer has no value, you should check like this before getting value of it:

    $customAttribute = $customer->getCustomAttribute('is_mobile_verified');
    if (!$customAttribute) {
        return '';
    }

    return $customAttribute->getValue();

Basically, the custom attribute value will be saved when you set a specific value to it and then save the customer. So to prevent the issue in case your custom attribute is not set, you should add a condition to verify it before use getValue() function

Cheers

  • Yes, I know this is one way. but in earlier Magento1.x we dont need to do a check like this, $customAttribute = $customerObj->getIsMobileVerified(); itself will return data if exists or empty value. Not an error. I am just looking is there a default Magento2 function to meet this – Butterfly Dec 27 '18 at 9:13
  • You should do this way in Magento2. – Babon Dec 27 '18 at 9:27

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