0

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 
 Vendor\\Module\\Model\\Data\\Customer::getAmount()

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

 if(!isset($customerObj->getAmount())){
    $amount= '';
}else{      
    $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

5
  • share code of data provider class Vendor\Module\Model\Data\Customer and
    – Amit Bera
    Sep 12, 2018 at 8:46
  • Yes, i have updated my question Sep 12, 2018 at 9:36
  • i have asking to provide code of Vendor\Module\Model\Data\Customer
    – Amit Bera
    Sep 12, 2018 at 9:37
  • If you have using repository then you must have data provider
    – Amit Bera
    Sep 12, 2018 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 Sep 12, 2018 at 9:44

2 Answers 2

1

Try Below code

$customerAmount=$customerObj->getCustomAttribute('bu_address1');

result will be

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

)

And for

$customerAmount=$customerObj->getCustomAttribute('bu_address1')->getValue();

Result will be

 Noida
4
  • 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 Sep 12, 2018 at 9:28
  • may i know what class is for this object $this->customerRepository in your code ? Sep 12, 2018 at 9:29
  • I am using \Magento\Customer\Api\CustomerRepositoryInterface Sep 12, 2018 at 9:30
  • you have done some thing wrong in attribute create see i have updated answer in post Sep 12, 2018 at 10:07
1
$customerAmount = $customer->getAttribute('amount') //or whatever the key is
$customerAmount = isset($customerAmount) ? $customerAmount : '';
4
  • same again, for me amount is not coming in object, so how can i check that condition? Above code didn't work for me Sep 12, 2018 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? Sep 12, 2018 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 Sep 12, 2018 at 9:26
  • how did you create it. how can you be sure it's created properly? Sep 12, 2018 at 15:28

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.