Address region - inconsistent value of isRequired property causes “Call to a member function getRegion() on null” error

magento.stackexchange https://magento.stackexchange.com/questions/330345

  •  15-04-2021
  •  | 
  •  

Question

When creating a new account(user registration) I'm getting the following error. I have noticed it after update to 2.3.6

Fatal error: Uncaught Error: Call to a member function getRegion() on null in /vendor/magento/module-customer/Model/Address.php:149 
Stack trace: 
#0 /generated/code/Magento/Customer/Model/Address/Interceptor.php(24): Magento\Customer\Model\Address->updateData(Object(Magento\Customer\Model\Data\Address)) 
#1 /vendor/magento/module-customer/Model/ResourceModel/AddressRepository.php(124): Magento\Customer\Model\Address\Interceptor->updateData(Object(Magento\Customer\Model\Data\Address)) 
#2 /vendor/magento/module-customer/Model/ResourceModel/CustomerRepository.php(272): Magento\Customer\Model\ResourceModel\AddressRepository->save(Object(Magento\Customer\Model\Data\Address)) 
#3 /vendor/magento/framework/Interception/Interceptor.php(58): Magento\Customer\Model\ResourceModel\CustomerRepository->save(Object(Magento\Customer\Model\Data\Customer), '(...)') 
#4 /vendor/magento/framework/Interception/Interceptor in /vendor/magento/module-customer/Model/Address.php on line 149

After clearing cache the error disappears, then reappears after registering several test users.

The region for a country I'm using is disabled:

  • in "Stores>Configuration>General>General>State Options" the country is not selected;
  • "Allow to Choose State if It is Optional for Country" is set to "No".

A foreach loop constructing used address methods in \vendor\magento\framework\Reflection\DataObjectProcessor.php: DataObjectProcessor->buildOutputDataArray(), should skip a method if its returned value is null and it is not required:

if ($value === null && !$isMethodReturnValueRequired) { continue; }

Inside foreach loop I placed $logger->info(json_encode($methods['getRegion']));.

After clearing cache, when error does not occur, this outputs:

{"type":"\\Magento\\Customer\\Api\\Data\\RegionInterface","isRequired":false,"description":null,"parameterCount":0}

and

{"type":"string","isRequired":true,"description":null,"parameterCount":0}

When the error occurs, it outputs:

{"type":"\\Magento\\Customer\\Api\\Data\\RegionInterface","isRequired":true,"description":null,"parameterCount":0}

isRequired key is taken from \vendor\magento\framework\Reflection\MethodsMap.php: MethodsMap->getMethodsMap() which seems to retrieve it from cache. Hence when the cache is cleared the error disappears.

My attempt to quick-fix it is to change the fragment that trows exception in \vendor\magento\module-customer\Model\Address.php: Address->updateData() from:

if (AddressInterface::REGION === $attributeCode) {
   $this->setRegion($address->getRegion()->getRegion());

to:

if (AddressInterface::REGION === $attributeCode && $address->getRegion()) {
   $this->setRegion($address->getRegion()->getRegion());

Do anybody else have such problem - I would like to know if it's a core bug?

If it's not a core bug can there be a better way to solve this, what else can possibly cause it?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top