@@ -206,17 +206,17 @@ public function save(CustomerInterface $customer, $passwordHash = null)
206206 /** @var NewOperation|null $delegatedNewOperation */
207207 $ delegatedNewOperation = !$ customer ->getId () ? $ this ->delegatedStorage ->consumeNewOperation () : null ;
208208 $ prevCustomerData = $ prevCustomerDataArr = null ;
209- if ($ customer ->getDefaultBilling ()) {
210- $ this ->validateDefaultAddress ($ customer , CustomerInterface::DEFAULT_BILLING );
211- }
212- if ($ customer ->getDefaultShipping ()) {
213- $ this ->validateDefaultAddress ($ customer , CustomerInterface::DEFAULT_SHIPPING );
214- }
215209 if ($ customer ->getId ()) {
216210 $ prevCustomerData = $ this ->getById ($ customer ->getId ());
217211 $ prevCustomerDataArr = $ this ->prepareCustomerData ($ prevCustomerData ->__toArray ());
218212 $ customer ->setCreatedAt ($ prevCustomerData ->getCreatedAt ());
219213 }
214+ if ($ customer ->getDefaultBilling ()) {
215+ $ this ->validateDefaultAddress ($ customer , $ prevCustomerData , CustomerInterface::DEFAULT_BILLING );
216+ }
217+ if ($ customer ->getDefaultShipping ()) {
218+ $ this ->validateDefaultAddress ($ customer , $ prevCustomerData , CustomerInterface::DEFAULT_SHIPPING );
219+ }
220220 /** @var $customer \Magento\Customer\Model\Data\Customer */
221221 $ customerArr = $ customer ->__toArray ();
222222 $ customer = $ this ->imageProcessor ->save (
@@ -569,28 +569,24 @@ private function prepareCustomerData(array $customerData): array
569569 * To validate default address
570570 *
571571 * @param CustomerInterface $customer
572+ * @param CustomerInterface|null $prevCustomerData
572573 * @param string $defaultAddressType
573574 * @return void
574575 * @throws InputException
575576 */
576577 private function validateDefaultAddress (
577578 CustomerInterface $ customer ,
579+ ?CustomerInterface $ prevCustomerData ,
578580 string $ defaultAddressType
579581 ): void {
580582 $ defaultAddressId = $ defaultAddressType === CustomerInterface::DEFAULT_BILLING ?
581583 (int ) $ customer ->getDefaultBilling () : (int ) $ customer ->getDefaultShipping ();
582-
583- if ($ customer ->getAddresses ()) {
584- foreach ($ customer ->getAddresses () as $ address ) {
585- $ addressArray = $ address ->__toArray ();
586- $ addressId = (int ) $ address ->getId ();
587- if (!empty ($ addressArray [$ defaultAddressType ])
588- || empty ($ addressId )
589- || $ defaultAddressId === $ addressId ) {
584+ if ($ prevCustomerData && $ prevCustomerData ->getAddresses ()) {
585+ foreach ($ prevCustomerData ->getAddresses () as $ address ) {
586+ if ($ defaultAddressId === (int ) $ address ->getId ()) {
590587 return ;
591588 }
592589 }
593-
594590 throw new InputException (
595591 __ (
596592 'The %fieldName value is invalid. Set the correct value and try again. ' ,
0 commit comments