4444use craft \commerce \records \Transaction as TransactionRecord ;
4545use craft \commerce \validators \StoreCountryValidator ;
4646use craft \db \Query ;
47+ use craft \elements \Address ;
4748use craft \elements \Address as AddressElement ;
4849use craft \elements \User ;
4950use craft \errors \ElementNotFoundException ;
@@ -1433,29 +1434,38 @@ protected function defineRules(): array
14331434 /**
14341435 * Automatically set addresses on the order if it's a cart and `autoSetNewCartAddresses` is `true`.
14351436 *
1436- * @return void
1437+ * @return bool
14371438 * @since 3.4.14
14381439 */
1439- public function autoSetAddresses (): void
1440+ public function autoSetAddresses (): bool
14401441 {
14411442 if ($ this ->isCompleted || !Plugin::getInstance ()->getSettings ()->autoSetNewCartAddresses ) {
1442- return ;
1443+ return false ;
14431444 }
14441445
1446+ /** @var User|CustomerBehavior|null $user */
14451447 $ user = $ this ->getCustomer ();
1448+ if (!$ user ) {
1449+ return false ;
1450+ }
14461451
1447- if (!$ this ->_shippingAddress && $ user ) {
1448- /** @var User|CustomerBehavior $user */
1449- if ($ primaryShippingAddressId = $ user ->getPrimaryShippingAddressId ()) {
1450- $ this ->shippingAddressId = $ primaryShippingAddressId ;
1451- }
1452+ $ autoSetOccurred = false ;
1453+
1454+ if (!$ this ->_shippingAddress && $ primaryShippingAddress = $ user ->getPrimaryShippingAddress ()) {
1455+ $ this ->sourceShippingAddressId = $ primaryShippingAddress ->id ;
1456+ $ shippingAddress = Craft::$ app ->getElements ()->duplicateElement ($ primaryShippingAddress , ['ownerId ' => $ this ->id ]);
1457+ $ this ->setShippingAddress ($ shippingAddress );
1458+ $ autoSetOccurred = true ;
14521459 }
14531460
1454- if (!$ this ->_billingAddress && $ user ) {
1455- if ($ primaryBillingAddressId = $ user ->getPrimaryBillingAddressId ()) {
1456- $ this ->billingAddressId = $ primaryBillingAddressId ;
1457- }
1461+ if (!$ this ->_billingAddress && $ primaryBillingAddress = $ user ->getPrimaryBillingAddress ()) {
1462+ $ this ->sourceBillingAddressId = $ primaryBillingAddress ->id ;
1463+ $ billingAddress = Craft::$ app ->getElements ()->duplicateElement ($ primaryBillingAddress , ['ownerId ' => $ this ->id ]);
1464+ $ this ->setBillingAddress ($ billingAddress );
1465+ $ autoSetOccurred = true ;
14581466 }
1467+
1468+ return $ autoSetOccurred ;
14591469 }
14601470
14611471 /**
@@ -1964,6 +1974,7 @@ public function afterSave(bool $isNew): void
19641974 $ currentUserIsCustomer = ($ currentUser && $ this ->getCustomer () && $ currentUser ->id == $ this ->getCustomer ()->id );
19651975
19661976 if ($ shippingAddress = $ this ->getShippingAddress ()) {
1977+ $ shippingAddress ->ownerId = $ this ->id ; // Always ensure the address is owned by the order
19671978 Craft::$ app ->getElements ()->saveElement ($ shippingAddress , false );
19681979 $ orderRecord ->shippingAddressId = $ shippingAddress ->id ;
19691980 $ this ->setShippingAddress ($ shippingAddress );
@@ -1977,6 +1988,7 @@ public function afterSave(bool $isNew): void
19771988 }
19781989
19791990 if ($ billingAddress = $ this ->getBillingAddress ()) {
1991+ $ billingAddress ->ownerId = $ this ->id ; // Always ensure the address is owned by the order
19801992 Craft::$ app ->getElements ()->saveElement ($ billingAddress , false );
19811993 $ orderRecord ->billingAddressId = $ billingAddress ->id ;
19821994 $ this ->setBillingAddress ($ billingAddress );
@@ -1990,6 +2002,7 @@ public function afterSave(bool $isNew): void
19902002 }
19912003
19922004 if ($ estimatedShippingAddress = $ this ->getEstimatedShippingAddress ()) {
2005+ $ estimatedShippingAddress ->ownerId = $ this ->id ; // Always ensure the address is owned by the order
19932006 Craft::$ app ->getElements ()->saveElement ($ estimatedShippingAddress , false );
19942007 $ orderRecord ->estimatedShippingAddressId = $ estimatedShippingAddress ->id ;
19952008 $ this ->setEstimatedShippingAddress ($ estimatedShippingAddress );
@@ -2002,6 +2015,7 @@ public function afterSave(bool $isNew): void
20022015 }
20032016
20042017 if (!$ this ->estimatedBillingSameAsShipping && $ estimatedBillingAddress = $ this ->getEstimatedBillingAddress ()) {
2018+ $ estimatedBillingAddress ->ownerId = $ this ->id ; // Always ensure the address is owned by the order
20052019 Craft::$ app ->getElements ()->saveElement ($ estimatedBillingAddress , false );
20062020 $ orderRecord ->estimatedBillingAddressId = $ estimatedBillingAddress ->id ;
20072021 $ this ->setEstimatedBillingAddress ($ estimatedBillingAddress );
0 commit comments