protected function CartManager::completeSaleAccount in Ubercart 8.4
Link a completed sale to a user.
Parameters
\Drupal\uc_order\OrderInterface $order: The order entity that has just been completed.
1 call to CartManager::completeSaleAccount()
- CartManager::completeSale in uc_cart/
src/ CartManager.php - Completes a sale, including adjusting order status and creating an account.
File
- uc_cart/
src/ CartManager.php, line 131
Class
- CartManager
- Provides the cart manager service.
Namespace
Drupal\uc_cartCode
protected function completeSaleAccount(OrderInterface $order) {
// Order already has a user ID, so the user was logged in during checkout.
if ($order
->getOwnerId()) {
$order->data->complete_sale = 'logged_in';
return;
}
// Email address matches an existing account.
if ($account = user_load_by_mail($order
->getEmail())) {
$order
->setOwner($account);
$order->data->complete_sale = 'existing_user';
return;
}
// Set up a new user.
$cart_config = \Drupal::config('uc_cart.settings');
$fields = [
'name' => uc_store_email_to_username($order
->getEmail()),
'mail' => $order
->getEmail(),
'init' => $order
->getEmail(),
// @todo Replace the following line with:
// 'pass' => \Drupal::service('password_generator')->generate(),
// when Drupal 9.1 becomes the lowest-supported version of Drupal.
// @see https://www.drupal.org/project/ubercart/issues/3217319
'pass' => user_password(),
'roles' => [],
'status' => $cart_config
->get('new_customer_status_active') ? 1 : 0,
];
// Override the username, if specified.
if (isset($order->data->new_user_name)) {
$fields['name'] = $order->data->new_user_name;
}
// Create the account.
$account = User::create($fields);
$account
->save();
// Override the password, if specified.
if (isset($order->data->new_user_hash)) {
\Drupal::database()
->query('UPDATE {users_field_data} SET pass = :hash WHERE uid = :uid', [
':hash' => $order->data->new_user_hash,
':uid' => $account
->id(),
]);
$account->password = t('Your password');
}
else {
$account->password = $fields['pass'];
$order->password = $fields['pass'];
}
// Send the customer their account details if enabled.
if ($cart_config
->get('new_customer_email')) {
$type = $cart_config
->get('new_customer_status_active') ? 'register_no_approval_required' : 'register_pending_approval';
\Drupal::service('plugin.manager.mail')
->mail('user', $type, $order
->getEmail(), uc_store_mail_recipient_langcode($order
->getEmail()), [
'account' => $account,
], uc_store_email_from());
}
$order
->setOwner($account);
$order->data->new_user_name = $fields['name'];
$order->data->complete_sale = 'new_user';
}