From ad5f3fa48e0402702d0b1339f8737028dc8aa27c Mon Sep 17 00:00:00 2001 From: OlgaVasyltsun Date: Wed, 16 Dec 2020 11:36:31 +0200 Subject: [PATCH 1/3] MC-39765: No such entity with addressId, occurs randomly on visitors browser. System Log Generated --- .../Observer/EmulateCustomerObserver.php | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php b/app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php index 8429eabd19e8a..0b978b9822345 100644 --- a/app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php +++ b/app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php @@ -6,6 +6,7 @@ namespace Magento\Persistent\Observer; use Magento\Framework\Event\ObserverInterface; +use Magento\Framework\Exception\NoSuchEntityException; /** * Class EmulateCustomer @@ -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(), @@ -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, @@ -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; + } + } } From 3bb857f8d60e45a40931bb99085c7d8f841ce922 Mon Sep 17 00:00:00 2001 From: OlgaVasyltsun Date: Wed, 16 Dec 2020 14:44:52 +0200 Subject: [PATCH 2/3] MC-39765: No such entity with addressId, occurs randomly on visitors browser. System Log Generated --- .../Magento/Persistent/Observer/EmulateCustomerObserver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php b/app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php index 0b978b9822345..c991836a287d2 100644 --- a/app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php +++ b/app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php @@ -103,7 +103,7 @@ public function execute(\Magento\Framework\Event\Observer $observer) } if ($defaultBilling = $customer->getDefaultBilling()) { - $address = $this->getCustomerAddressById($defaultShipping); + $address = $this->getCustomerAddressById($defaultBilling); if ($address !== null) { $this->_customerSession->setDefaultTaxBillingAddress([ From 92ecbbfeabd9edd0b137915f61bbacfa123a7e8e Mon Sep 17 00:00:00 2001 From: OlgaVasyltsun Date: Wed, 16 Dec 2020 17:21:05 +0200 Subject: [PATCH 3/3] MC-39765: No such entity with addressId, occurs randomly on visitors browser. System Log Generated --- .../Magento/Persistent/Observer/EmulateCustomerObserver.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php b/app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php index c991836a287d2..1ff81137de57b 100644 --- a/app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php +++ b/app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php @@ -87,7 +87,7 @@ 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()) { - $address = $this->getCustomerAddressById($defaultShipping); + $address = $this->getCustomerAddressById((int) $defaultShipping); if ($address !== null) { $this->_customerSession->setDefaultTaxShippingAddress( @@ -103,7 +103,7 @@ public function execute(\Magento\Framework\Event\Observer $observer) } if ($defaultBilling = $customer->getDefaultBilling()) { - $address = $this->getCustomerAddressById($defaultBilling); + $address = $this->getCustomerAddressById((int) $defaultBilling); if ($address !== null) { $this->_customerSession->setDefaultTaxBillingAddress([ @@ -127,7 +127,7 @@ public function execute(\Magento\Framework\Event\Observer $observer) * @param int $addressId * @return \Magento\Customer\Api\Data\AddressInterface|null */ - private function getCustomerAddressById($addressId) + private function getCustomerAddressById(int $addressId) { try { return $this->addressRepository->getById($addressId);