Skip to content

Commit

Permalink
MC-39765: No such entity with addressId, occurs randomly on visitors …
Browse files Browse the repository at this point in the history
…browser. System Log Generated
  • Loading branch information
OlgaVasyltsun committed Dec 16, 2020
1 parent ad29452 commit ad5f3fa
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Persistent\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Class EmulateCustomer
Expand Down Expand Up @@ -86,9 +87,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
$customer = $this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId());
if ($defaultShipping = $customer->getDefaultShipping()) {
/** @var \Magento\Customer\Model\Data\Address $address */
$address = $this->addressRepository->getById($defaultShipping);
if ($address) {
$address = $this->getCustomerAddressById($defaultShipping);

if ($address !== null) {
$this->_customerSession->setDefaultTaxShippingAddress(
[
'country_id' => $address->getCountryId(),
Expand All @@ -102,8 +103,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
}

if ($defaultBilling = $customer->getDefaultBilling()) {
$address = $this->addressRepository->getById($defaultBilling);
if ($address) {
$address = $this->getCustomerAddressById($defaultShipping);

if ($address !== null) {
$this->_customerSession->setDefaultTaxBillingAddress([
'country_id' => $address->getCountryId(),
'region_id' => $address->getRegion() ? $address->getRegionId() : null,
Expand All @@ -118,4 +120,19 @@ public function execute(\Magento\Framework\Event\Observer $observer)
}
return $this;
}

/**
* Returns customer address by id
*
* @param int $addressId
* @return \Magento\Customer\Api\Data\AddressInterface|null
*/
private function getCustomerAddressById($addressId)
{
try {
return $this->addressRepository->getById($addressId);
} catch (NoSuchEntityException $exception) {
return null;
}
}
}

0 comments on commit ad5f3fa

Please sign in to comment.