diff --git a/classes/Context.php b/classes/Context.php index c6d21c4de..fa672b884 100644 --- a/classes/Context.php +++ b/classes/Context.php @@ -288,4 +288,85 @@ class ContextCore { return clone($this); } + + public function updateCustomer(Customer $customer, $loginCustomer = 0) + { + $this->customer = $customer; + $this->cookie->id_customer = (int) $customer->id; + $this->cookie->customer_lastname = $customer->lastname; + $this->cookie->customer_firstname = $customer->firstname; + $this->cookie->passwd = $customer->passwd; + $this->cookie->logged = true; + $customer->logged = true; + $this->cookie->email = $customer->email; + $this->cookie->is_guest = $customer->isGuest(); + + $currentCookieGuest = $this->cookie->id_guest; + + if (Configuration::get('PS_CART_FOLLOWING') && (empty($this->cookie->id_cart) || Cart::getNbProducts((int) $this->cookie->id_cart) == 0) && $idCart = (int) Cart::lastNoneOrderedCart($this->customer->id)) { + $this->cart = new Cart($idCart); + $this->cart->secure_key = $customer->secure_key; + if (!$loginCustomer) { + $this->cookie->id_guest = (int) $this->cart->id_guest; + } + } else { + if (!isset($this->cookie->id_guest)) { + Guest::setNewGuest($this->cookie); + } + + if (Validate::isLoadedObject($this->cart)) { + $idCarrier = (int) $this->cart->id_carrier; + $this->cart->secure_key = $customer->secure_key; + $this->cart->id_guest = (int) $this->cookie->id_guest; + $this->cart->id_carrier = 0; + if (!empty($idCarrier)) { + $deliveryOption = [$this->cart->id_address_delivery => $idCarrier . ',']; + $this->cart->setDeliveryOption($deliveryOption); + } else { + $this->cart->setDeliveryOption(null); + } + $this->cart->id_customer = (int) $customer->id; + $this->cart->updateAddressId($this->cart->id_address_delivery, (int) Address::getFirstCustomerAddressId((int) ($customer->id))); + $this->cart->id_address_delivery = (int) Address::getFirstCustomerAddressId((int) ($customer->id)); + $this->cart->id_address_invoice = (int) Address::getFirstCustomerAddressId((int) ($customer->id)); + + // update id guest in htl_cart_booking_data for bookings added to the cart as a visitor + $objCartBookingData = new HotelCartBookingData(); + if ($hotelCartBookings = $objCartBookingData->getCartCurrentDataByCartId($this->cart->id)) { + foreach ($hotelCartBookings as $cartBooking) { + $objCartBookingData = new HotelCartBookingData($cartBooking['id']); + $objCartBookingData->id_guest = (int) $this->cookie->id_guest; + $objCartBookingData->id_customer = (int) $customer->id; + $objCartBookingData->save(); + } + } + } + } + if (Validate::isLoadedObject($this->cart)) { + $this->cart->save(); + $this->cart->autosetProductAddress(); + + $this->cookie->id_cart = (int) $this->cart->id; + } + + // if customer is login to account + if ($loginCustomer) { + // Update or merge the guest with the customer id (login and account creation) + $objGuest = new Guest($currentCookieGuest); + if ($customerGuestId = Guest::getFromCustomer((int)$this->cookie->id_customer)) { + if ($objGuest->id != $customerGuestId) { + // The new guest is merged with the old one when it's connecting to an account + $objGuest->mergeWithCustomer($customerGuestId, $this->cookie->id_customer); + + // update the id_guest in the cookie + $this->cookie->id_guest = $customerGuestId; + } + } else { + $objGuest->id_customer = (int)$this->cookie->id_customer; + $objGuest->update(); + } + } + + $this->cookie->write(); + } } diff --git a/classes/Guest.php b/classes/Guest.php index 92adb0a76..15a71165c 100644 --- a/classes/Guest.php +++ b/classes/Guest.php @@ -160,30 +160,35 @@ class GuestCore extends ObjectModel if (!Validate::isUnsignedId($id_customer)) { return false; } - $result = Db::getInstance()->getRow(' - SELECT `id_guest` - FROM `'._DB_PREFIX_.'guest` - WHERE `id_customer` = '.(int)($id_customer)); - return $result['id_guest']; + + if ($result = Db::getInstance()->getRow( + 'SELECT `id_guest` FROM `'._DB_PREFIX_.'guest` WHERE `id_customer` = '.(int)($id_customer) + )) { + return $result['id_guest']; + } + + return false; } - public function mergeWithCustomer($id_guest, $id_customer) + public function mergeWithCustomer($idGuest, $idCustomer) { // Since the guests are merged, the guest id in the connections table must be changed too - Db::getInstance()->execute(' - UPDATE `'._DB_PREFIX_.'connections` c - SET c.`id_guest` = '.(int)($id_guest).' - WHERE c.`id_guest` = '.(int)($this->id)); + Db::getInstance()->update('connections', [ + 'id_guest' => (int) $idGuest, + ], 'id_guest = ' . (int) $this->id); - // The current guest is removed from the database + // Since the guests are merged, the guest id in the cart table must be changed too + Db::getInstance()->update('cart', [ + 'id_guest' => (int) $idGuest, + ], 'id_guest = ' . (int) $this->id); + + // Since the guests are merged, the guest id in the htl_cart_booking_data table must be changed too + Db::getInstance()->update('htl_cart_booking_data', [ + 'id_guest' => (int) $idGuest, + ], 'id_guest = ' . (int) $this->id); + + // The current guest is removed from the database because now we have old customer guest id $this->delete(); - - // $this is still filled with values, so it's id is changed for the old guest - $this->id = (int)($id_guest); - $this->id_customer = (int)($id_customer); - - // $this is now the old guest but filled with the most up to date values - $this->update(); } public static function setNewGuest($cookie) diff --git a/classes/controller/FrontController.php b/classes/controller/FrontController.php index cdd22e7a8..fed3d2679 100644 --- a/classes/controller/FrontController.php +++ b/classes/controller/FrontController.php @@ -1528,6 +1528,7 @@ class FrontControllerCore extends Controller $this->context->cookie->is_guest = $customer->isGuest(); $this->context->cookie->passwd = $customer->passwd; $this->context->cookie->email = $customer->email; + $this->context->cookie->id_guest = (int) $cart->id_guest; return $id_cart; } } diff --git a/controllers/front/AuthController.php b/controllers/front/AuthController.php index 32ab62ac7..611a1531b 100644 --- a/controllers/front/AuthController.php +++ b/controllers/front/AuthController.php @@ -299,40 +299,7 @@ class AuthControllerCore extends FrontController } elseif (!$authentication || !$customer->id) { $this->errors[] = Tools::displayError('Authentication failed.'); } else { - $this->context->cookie->id_compare = isset($this->context->cookie->id_compare) ? $this->context->cookie->id_compare: CompareProduct::getIdCompareByIdCustomer($customer->id); - $this->context->cookie->id_customer = (int)($customer->id); - $this->context->cookie->customer_lastname = $customer->lastname; - $this->context->cookie->customer_firstname = $customer->firstname; - $this->context->cookie->logged = 1; - $customer->logged = 1; - $this->context->cookie->is_guest = $customer->isGuest(); - $this->context->cookie->passwd = $customer->passwd; - $this->context->cookie->email = $customer->email; - - // Add customer to the context - $this->context->customer = $customer; - - if (Configuration::get('PS_CART_FOLLOWING') && (empty($this->context->cookie->id_cart) || Cart::getNbProducts($this->context->cookie->id_cart) == 0) && $id_cart = (int)Cart::lastNoneOrderedCart($this->context->customer->id)) { - $this->context->cart = new Cart($id_cart); - } else { - $id_carrier = (int)$this->context->cart->id_carrier; - $this->context->cart->id_carrier = 0; - $this->context->cart->setDeliveryOption(null); - $this->context->cart->id_address_delivery = (int)Address::getFirstCustomerAddressId((int)($customer->id)); - $this->context->cart->id_address_invoice = (int)Address::getFirstCustomerAddressId((int)($customer->id)); - } - $this->context->cart->id_customer = (int)$customer->id; - $this->context->cart->secure_key = $customer->secure_key; - - if ($this->ajax && isset($id_carrier) && $id_carrier && Configuration::get('PS_ORDER_PROCESS_TYPE')) { - $delivery_option = array($this->context->cart->id_address_delivery => $id_carrier.','); - $this->context->cart->setDeliveryOption($delivery_option); - } - - $this->context->cart->save(); - $this->context->cookie->id_cart = (int)$this->context->cart->id; - $this->context->cookie->write(); - $this->context->cart->autosetProductAddress(); + $this->context->updateCustomer($customer, 1); Hook::exec('actionAuthentication', array('customer' => $this->context->customer)); @@ -508,7 +475,7 @@ class AuthControllerCore extends FrontController } } - $this->updateContext($customer); + $this->context->updateCustomer($customer, 1); $this->context->cart->update(); Hook::exec('actionCustomerAccountAdd', array( @@ -671,7 +638,7 @@ class AuthControllerCore extends FrontController // we add the guest customer in the guest customer group $customer->addGroups(array((int)Configuration::get('PS_GUEST_GROUP'))); } - $this->updateContext($customer); + $this->context->cart->id_address_delivery = (int)Address::getFirstCustomerAddressId((int)$customer->id); $this->context->cart->id_address_invoice = (int)Address::getFirstCustomerAddressId((int)$customer->id); if (isset($address_invoice) && Validate::isLoadedObject($address_invoice)) { @@ -683,6 +650,7 @@ class AuthControllerCore extends FrontController $this->context->cart->setDeliveryOption($delivery_option); } + $this->context->updateCustomer($customer, 1); // If a logged guest logs in as a customer, the cart secure key was already set and needs to be updated $this->context->cart->update(); @@ -786,30 +754,6 @@ class AuthControllerCore extends FrontController } } - /** - * Update context after customer creation - * @param Customer $customer Created customer - */ - protected function updateContext(Customer $customer) - { - $this->context->customer = $customer; - $this->context->smarty->assign('confirmation', 1); - $this->context->cookie->id_customer = (int)$customer->id; - $this->context->cookie->customer_lastname = $customer->lastname; - $this->context->cookie->customer_firstname = $customer->firstname; - $this->context->cookie->passwd = $customer->passwd; - $this->context->cookie->logged = 1; - // if register process is in two steps, we display a message to confirm account creation - if (!Configuration::get('PS_REGISTRATION_PROCESS_TYPE')) { - $this->context->cookie->account_created = 1; - } - $customer->logged = 1; - $this->context->cookie->email = $customer->email; - $this->context->cookie->is_guest = !Tools::getValue('is_new_customer', 1); - // Update cart address - $this->context->cart->secure_key = $customer->secure_key; - } - /** * sendConfirmationMail * @param Customer $customer diff --git a/controllers/front/OrderOpcController.php b/controllers/front/OrderOpcController.php index c45aa2d48..5aee90df5 100644 --- a/controllers/front/OrderOpcController.php +++ b/controllers/front/OrderOpcController.php @@ -149,7 +149,7 @@ class OrderOpcControllerCore extends ParentOrderController if (!count($this->errors)) { $customer = new Customer($this->context->customer->id); if ($customer->transformToCustomer($this->context->language->id, $passwd)) { - $this->updateContext($customer); + $this->context->updateCustomer($customer); } else { $this->errors[] = Tools::displayError('An error occurred while transforming your account into a registered customer.'); } @@ -1049,20 +1049,4 @@ class OrderOpcControllerCore extends ParentOrderController } $this->context->cart->save(); } - - public function updateContext(Customer $customer) - { - $this->context->cookie->id_customer = (int)$customer->id; - $this->context->cookie->customer_lastname = $customer->lastname; - $this->context->cookie->customer_firstname = $customer->firstname; - $this->context->cookie->passwd = $customer->passwd; - $this->context->cookie->logged = 1; - $this->context->cookie->email = $customer->email; - $this->context->cookie->is_guest = 0; - - $this->context->cart->secure_key = $customer->secure_key; - - $customer->logged = 1; - $this->context->customer = $customer; - } } diff --git a/controllers/front/PasswordController.php b/controllers/front/PasswordController.php index 317818c87..f5c8c38fd 100644 --- a/controllers/front/PasswordController.php +++ b/controllers/front/PasswordController.php @@ -84,6 +84,9 @@ class PasswordControllerCore extends FrontController '{passwd}' => $password ); if (Mail::Send($this->context->language->id, 'password', Mail::l('Your new password'), $mail_params, $customer->email, $customer->firstname.' '.$customer->lastname)) { + + $this->context->updateCustomer($customer); + $this->context->smarty->assign(array('confirmation' => 1, 'customer_email' => $customer->email)); } else { $this->errors[] = Tools::displayError('An error occurred while sending the email.'); diff --git a/modules/statsdata/statsdata.php b/modules/statsdata/statsdata.php index 3e36b4216..cdf36c865 100644 --- a/modules/statsdata/statsdata.php +++ b/modules/statsdata/statsdata.php @@ -49,9 +49,7 @@ class StatsData extends Module public function install() { return (parent::install() - && $this->registerHook('displayFooter') - && $this->registerHook('authentication') - && $this->registerHook('createAccount')); + && $this->registerHook('displayFooter')); } public function getContent() @@ -112,32 +110,6 @@ class StatsData extends Module return ''; } - public function hookCreateAccount($params) - { - return $this->hookAuthentication($params); - } - - public function hookAuthentication($params) - { - // Update or merge the guest with the customer id (login and account creation) - $guest = new Guest($params['cookie']->id_guest); - $result = Db::getInstance()->getRow(' - SELECT `id_guest` - FROM `'._DB_PREFIX_.'guest` - WHERE `id_customer` = '.(int)$params['cookie']->id_customer); - - if ((int)$result['id_guest']) { - // The new guest is merged with the old one when it's connecting to an account - $guest->mergeWithCustomer($result['id_guest'], $params['cookie']->id_customer); - $params['cookie']->id_guest = $guest->id; - } else { - // The guest is duplicated if it has multiple customer accounts - $method = ($guest->id_customer) ? 'add' : 'update'; - $guest->id_customer = $params['cookie']->id_customer; - $guest->{$method}(); - } - } - public function renderForm() { $fields_form = array(