Skip to content

Commit d4d51cb

Browse files
authored
Merge pull request #2812 from craftcms/feature/fix-2804
Fix bug where `autoSetNewCartAddresses` was not working for new carts.
2 parents 275a4c0 + e624e51 commit d4d51cb

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
- Example templates address forms now show plain text custom fields.
77

88
### Fixed
9+
- Fixed a bug where the `autoSetNewCartAddresses` setting did not work. ([#2804](https://github.com/craftcms/commerce/issues/2804))
910
- Fixed a PHP error that occurred when making a payment on the Edit Order page. ([#2795](https://github.com/craftcms/commerce/issues/2795))
11+
- Fixed a PHP error that occurred when duplicating addresses that are not owned by Users.
1012
- Fixed a bug when submitting a custom field on address namespace doesn't save the field. ([2809](https://github.com/craftcms/commerce/pull/2809))
1113

1214
## 4.0.0 - 2022-05-04

src/behaviors/CustomerAddressBehavior.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public function afterPropagate(): void
5454

5555
/** @var User $user */
5656
$user = $this->owner->getOwner();
57+
58+
if ($user === null || !$user instanceof User) {
59+
return;
60+
}
61+
5762
$customersService = Plugin::getInstance()->getCustomers();
5863

5964
$customer = $customersService->ensureCustomer($user);

src/elements/Order.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use craft\commerce\records\Transaction as TransactionRecord;
4545
use craft\commerce\validators\StoreCountryValidator;
4646
use craft\db\Query;
47+
use craft\elements\Address;
4748
use craft\elements\Address as AddressElement;
4849
use craft\elements\User;
4950
use 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);

src/services/Carts.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ public function getCart(bool $forceSave = false): Order
121121
if ($currentUser) {
122122
$this->_cart->setCustomer($currentUser); // Will ensure the email is also set
123123
}
124-
$this->_cart->autoSetAddresses();
124+
}
125+
126+
if ($this->_cart->autoSetAddresses()) {
127+
// If we are auto setting address on the cart, save the cart so addresses have an ID to belong to.
128+
$forceSave = true;
125129
}
126130

127131
// Ensure the session knows what the current cart is.

0 commit comments

Comments
 (0)