@@ -206,17 +206,17 @@ public function save(CustomerInterface $customer, $passwordHash = null)
206
206
/** @var NewOperation|null $delegatedNewOperation */
207
207
$ delegatedNewOperation = !$ customer ->getId () ? $ this ->delegatedStorage ->consumeNewOperation () : null ;
208
208
$ 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
- }
215
209
if ($ customer ->getId ()) {
216
210
$ prevCustomerData = $ this ->getById ($ customer ->getId ());
217
211
$ prevCustomerDataArr = $ this ->prepareCustomerData ($ prevCustomerData ->__toArray ());
218
212
$ customer ->setCreatedAt ($ prevCustomerData ->getCreatedAt ());
219
213
}
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
+ }
220
220
/** @var $customer \Magento\Customer\Model\Data\Customer */
221
221
$ customerArr = $ customer ->__toArray ();
222
222
$ customer = $ this ->imageProcessor ->save (
@@ -569,28 +569,24 @@ private function prepareCustomerData(array $customerData): array
569
569
* To validate default address
570
570
*
571
571
* @param CustomerInterface $customer
572
+ * @param CustomerInterface|null $prevCustomerData
572
573
* @param string $defaultAddressType
573
574
* @return void
574
575
* @throws InputException
575
576
*/
576
577
private function validateDefaultAddress (
577
578
CustomerInterface $ customer ,
579
+ ?CustomerInterface $ prevCustomerData ,
578
580
string $ defaultAddressType
579
581
): void {
580
582
$ defaultAddressId = $ defaultAddressType === CustomerInterface::DEFAULT_BILLING ?
581
583
(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 ()) {
590
587
return ;
591
588
}
592
589
}
593
-
594
590
throw new InputException (
595
591
__ (
596
592
'The %fieldName value is invalid. Set the correct value and try again. ' ,
0 commit comments