6
6
namespace Magento \Persistent \Observer ;
7
7
8
8
use Magento \Framework \Event \ObserverInterface ;
9
+ use Magento \Framework \Exception \NoSuchEntityException ;
9
10
10
11
/**
11
12
* Class EmulateCustomer
@@ -86,9 +87,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
86
87
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
87
88
$ customer = $ this ->customerRepository ->getById ($ this ->_persistentSession ->getSession ()->getCustomerId ());
88
89
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 ) {
92
93
$ this ->_customerSession ->setDefaultTaxShippingAddress (
93
94
[
94
95
'country_id ' => $ address ->getCountryId (),
@@ -102,8 +103,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
102
103
}
103
104
104
105
if ($ defaultBilling = $ customer ->getDefaultBilling ()) {
105
- $ address = $ this ->addressRepository ->getById ($ defaultBilling );
106
- if ($ address ) {
106
+ $ address = $ this ->getCustomerAddressById ((int ) $ defaultBilling );
107
+
108
+ if ($ address !== null ) {
107
109
$ this ->_customerSession ->setDefaultTaxBillingAddress ([
108
110
'country_id ' => $ address ->getCountryId (),
109
111
'region_id ' => $ address ->getRegion () ? $ address ->getRegionId () : null ,
@@ -118,4 +120,19 @@ public function execute(\Magento\Framework\Event\Observer $observer)
118
120
}
119
121
return $ this ;
120
122
}
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
+ }
121
138
}
0 commit comments