I am trying to retrieve customer data with customer Object.

Below is the code:-

protected $customerRepository;
public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
) {
   $this->customerRepository = $customerRepository;

public function getUser($userId) {
    $customerObj = $this->customerRepository->getById($userId);
    $customerEmail = $customerObj->getEmail();
    $customerAmount = $customerObj->getAmount();

I am getting below error when used above code:-

 Uncaught Error: Call to undefined method 

So I am using below code now to get amount value by thinking amount value is not set for that customer:-

    $amount= '';
    $amount= $customerObj->getAmount();

Now I am getting below error:-

  PHP Fatal error:  Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

How can I check the condition if that attribute value is not set, then I will set it as empty.

Is that can be checked? Please, anyone suggest me

  • share code of data provider class Vendor\Module\Model\Data\Customer and – Amit Bera Sep 12 '18 at 8:46
  • Yes, i have updated my question – jafar pinjar Sep 12 '18 at 9:36
  • i have asking to provide code of Vendor\Module\Model\Data\Customer – Amit Bera Sep 12 '18 at 9:37
  • If you have using repository then you must have data provider – Amit Bera Sep 12 '18 at 9:37
  • Got it, you are right Amit, I was not defined that attribute in Model\Data\Customer file, now i am getting value as null – jafar pinjar Sep 12 '18 at 9:44

Try Below code


result will be

Magento\Framework\Api\AttributeValue Object
    [_data:protected] => Array
            [attribute_code] => bu_address1
            [value] => Noida


And for


Result will be

| improve this answer | |
  • I am getting below error after using your code Call to a member function getValue() on null, I am printing customer object, i can not see any value called amount, so i need to check that – jafar pinjar Sep 12 '18 at 9:28
  • may i know what class is for this object $this->customerRepository in your code ? – Ansar Husain Sep 12 '18 at 9:29
  • I am using \Magento\Customer\Api\CustomerRepositoryInterface – jafar pinjar Sep 12 '18 at 9:30
  • you have done some thing wrong in attribute create see i have updated answer in post – Ansar Husain Sep 12 '18 at 10:07
$customerAmount = $customer->getAttribute('amount') //or whatever the key is
$customerAmount = isset($customerAmount) ? $customerAmount : '';
| improve this answer | |
  • same again, for me amount is not coming in object, so how can i check that condition? Above code didn't work for me – jafar pinjar Sep 12 '18 at 8:37
  • what do you mean "amount is not coming in object"? can you edit your question and show us how you created the attribute? – Philipp Sander Sep 12 '18 at 8:41
  • yes attribute is created properly but the value for that attribute is not set for particular customer, i need to check if that value exists, i need to show else i need to show as empty – jafar pinjar Sep 12 '18 at 9:26
  • how did you create it. how can you be sure it's created properly? – Philipp Sander Sep 12 '18 at 15:28

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.