1

I am writing an Observer to Magento\Customer\Account\Index but the Objects from class \Magento\Customer\Model\Customer return null when getExtensionAttributes is executed.

  $customer = $customerFactory->create()->load($customerID); 
  //$customerRepository->get($customerID);
    $ext = $customer->getExtensionAttributes();
    if($ext == null){
        throw new \Exception(" var ext = null");
    }

I used CustomerRepository and CustomerFactory to get $customer

2
  • Use repository instead of model class
    – Sohel Rana
    Jan 15, 2020 at 17:23
  • I used CustomerRepository to get $customer Jan 15, 2020 at 19:54

2 Answers 2

0

You don't need to use CustomerFactory for getting customer data.

CustomerRepository is enough for getting a customer data.

You need to changes the code if($ext == null){ to if($ext === null){.

//$customer = $customerFactory->create()->load($customerID); 
$customerRepository->get($customerID);
$ext = $customer->getExtensionAttributes();
if($ext === null){
    throw new \Exception(" var ext = null");
}

=== means Identical match condition which is vastly used at PHP 7,

See https://www.w3schools.com/php/php_operators.asp`

0

It is happening because the Magento\Customer\Model\Customer is not an Extensible Model (does not extend the \Magento\Framework\Api\AbstractExtensibleObject class), it extending the \Magento\Framework\Model\AbstractModel which is not an extensible object. You must use the repository, because it will return the instance of Magento\Customer\Model\Data\Customer which is extensible object.

Or you can use your code, but with Magento\Customer\Model\Data\CustomerFactory.

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.