66namespace Magento \Persistent \Observer ;
77
88use Magento \Framework \Event \ObserverInterface ;
9+ use Magento \Framework \Exception \NoSuchEntityException ;
910
1011/**
1112 * Class EmulateCustomer
@@ -86,9 +87,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
8687 /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
8788 $ customer = $ this ->customerRepository ->getById ($ this ->_persistentSession ->getSession ()->getCustomerId ());
8889 if ($ defaultShipping = $ customer ->getDefaultShipping ()) {
89- /** @var \Magento\Customer\Model\Data\Address $address */
90- $ address = $ this -> addressRepository -> getById ( $ defaultShipping );
91- if ($ address ) {
90+ $ address = $ this -> getCustomerAddressById (( int ) $ defaultShipping );
91+
92+ if ($ address !== null ) {
9293 $ this ->_customerSession ->setDefaultTaxShippingAddress (
9394 [
9495 'country_id ' => $ address ->getCountryId (),
@@ -102,8 +103,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
102103 }
103104
104105 if ($ defaultBilling = $ customer ->getDefaultBilling ()) {
105- $ address = $ this ->addressRepository ->getById ($ defaultBilling );
106- if ($ address ) {
106+ $ address = $ this ->getCustomerAddressById ((int ) $ defaultBilling );
107+
108+ if ($ address !== null ) {
107109 $ this ->_customerSession ->setDefaultTaxBillingAddress ([
108110 'country_id ' => $ address ->getCountryId (),
109111 'region_id ' => $ address ->getRegion () ? $ address ->getRegionId () : null ,
@@ -118,4 +120,19 @@ public function execute(\Magento\Framework\Event\Observer $observer)
118120 }
119121 return $ this ;
120122 }
123+
124+ /**
125+ * Returns customer address by id
126+ *
127+ * @param int $addressId
128+ * @return \Magento\Customer\Api\Data\AddressInterface|null
129+ */
130+ private function getCustomerAddressById (int $ addressId )
131+ {
132+ try {
133+ return $ this ->addressRepository ->getById ($ addressId );
134+ } catch (NoSuchEntityException $ exception ) {
135+ return null ;
136+ }
137+ }
121138}
0 commit comments