Skip to content

Commit

Permalink
Copy addresses to user when registering during checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Jun 23, 2023
1 parent 6fd05ee commit 7ba4ff3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 4.3.0 - Unreleased

- PLACEHOLDER
- Guest customers registering during checkout now have their addresses saved to their account.

## Unreleased

Expand Down
45 changes: 44 additions & 1 deletion src/services/Customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,11 @@ private function _activateUserFromOrder(Order $order): void
$user = Craft::$app->getUsers()->ensureUserByEmail($order->email);

if (!$user->getIsCredentialed()) {
$billingAddress = $order->getBillingAddress();
$shippingAddress = $order->getShippingAddress();

if (!$user->fullName) {
$user->fullName = $order->getBillingAddress()?->fullName ?? $order->getShippingAddress()?->fullName ?? '';
$user->fullName = $billingAddress?->fullName ?? $shippingAddress?->fullName ?? '';
}

$user->username = $order->email;
Expand All @@ -334,6 +337,46 @@ private function _activateUserFromOrder(Order $order): void
if (!$emailSent) {
Craft::warning('"registerUserOnOrderComplete" used to create the user, but couldn’t send an activation email. Check your email settings.', __METHOD__);
}

if ($billingAddress || $shippingAddress) {
$newAttributes = ['ownerId' => $user->id];

// If there is only one address make sure we don't add duplicates to the user
if ($order->hasMatchingAddresses()) {
$newAttributes['title'] = Craft::t('commerce', 'Address');
$shippingAddress = null;
}

// Copy addresses to user
if ($billingAddress) {
$newBillingAddress = Craft::$app->getElements()->duplicateElement($billingAddress, $newAttributes);

/**
* Because we are cloning from an order address the `CustomerAddressBehavior` hasn't been instantiated
* therefore we are unable to simply set the `isPrimaryBilling` property when specifying the new attributes during duplication.
*/
if (!$newBillingAddress->hasErrors()) {
$this->savePrimaryBillingAddressId($user, $newBillingAddress->id);

if ($order->hasMatchingAddresses()) {
$this->savePrimaryShippingAddressId($user, $newBillingAddress->id);
}
}
}

if ($shippingAddress) {
$newShippingAddress = Craft::$app->getElements()->duplicateElement($shippingAddress, $newAttributes);

/**
* Because we are cloning from an order address the `CustomerAddressBehavior` hasn't been instantiated
* therefore we are unable to simply set the `isPrimaryShipping` property when specifying the new attributes during duplication.
*/
if (!$newShippingAddress->hasErrors()) {
$this->savePrimaryShippingAddressId($user, $newShippingAddress->id);
}
}
}

} else {
$errors = $user->getErrors();
Craft::warning('Could not create user on order completion.', __METHOD__);
Expand Down
1 change: 1 addition & 0 deletions src/translations/en/commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'Address Line 2' => 'Address Line 2',
'Address Updated.' => 'Address Updated.',
'Address not found.' => 'Address not found.',
'Address' => 'Address',
'Adjust price when included rate is disqualified?' => 'Adjust price when included rate is disqualified?',
'Adjustments' => 'Adjustments',
'All Orders' => 'All Orders',
Expand Down

0 comments on commit 7ba4ff3

Please sign in to comment.