diff --git a/classes/Address.php b/classes/Address.php index b3f87622f..d53060765 100644 --- a/classes/Address.php +++ b/classes/Address.php @@ -294,6 +294,9 @@ class AddressCore extends ObjectModel public static function getFirstCustomerAddressId($id_customer, $active = true) { + if (!$id_customer) + return false; + return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT `id_address` FROM `'._DB_PREFIX_.'address` diff --git a/classes/Cart.php b/classes/Cart.php index 7bd843b38..6f7beba10 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -736,6 +736,14 @@ class CartCore extends ObjectModel { if (!$shop) $shop = Context::getContext()->shop; + + if (Context::getContext()->customer->id) + { + if ($id_address_delivery == 0) // The $id_address_delivery is null, get the default customer address + $id_address_delivery = (int)Address::getFirstCustomerAddressId((int)Context::getContext()->customer->id); + else if (!Customer::customerHasAddress(Context::getContext()->customer->id, $id_address_delivery)) // The $id_address_delivery must be linked with customer + $id_address_delivery = 0; + } $quantity = (int)$quantity; $id_product = (int)$id_product; @@ -2972,32 +2980,19 @@ class CartCore extends ObjectModel */ public function autosetProductAddress() { + $id_address_delivery = 0; // Get the main address of the customer if ((int)$this->id_address_delivery > 0) - $id_address_deivery = (int)$this->id_address_delivery; + $id_address_delivery = (int)$this->id_address_delivery; else - { - if ((int)$cart->id_customer == 0) - return; - - $customer = new Customer((int)$cart->id_customer); - $addresses = $customer->getAddresses(Context::getContext()->language->id); - - if (count($addresses) == 0) - return; - - $id_address_delivery = $addresses[0]['id_address']; - } - + $id_address_delivery = (int)Address::getFirstCustomerAddressId(Context::getContext()->customer->id); + + if (!$id_address_delivery) + return; + // Update $sql = 'UPDATE `'._DB_PREFIX_.'cart_product` - SET `id_address_delivery` = - ( - SELECT `id_address_delivery` - FROM `'._DB_PREFIX_.'cart` - WHERE `id_cart` = '.(int)$this->id.' - AND `id_shop` = '.(int)$this->id_shop.' - ) + SET `id_address_delivery` = '.(int)$id_address_delivery.' WHERE `id_cart` = '.(int)$this->id.' AND (`id_address_delivery` = 0 OR `id_address_delivery` IS NULL) AND `id_shop` = '.(int)$this->id_shop; diff --git a/classes/Customer.php b/classes/Customer.php index 08f0762d6..7fa966954 100644 --- a/classes/Customer.php +++ b/classes/Customer.php @@ -200,10 +200,10 @@ class CustomerCore extends ObjectModel } /** - * Return customers list - * - * @return array Customers - */ + * Return customers list + * + * @return array Customers + */ public static function getCustomers(Shop $shop = null) { if (!$shop) @@ -217,21 +217,21 @@ class CustomerCore extends ObjectModel } /** - * Return customer instance from its e-mail (optionnaly check password) - * - * @param string $email e-mail - * @param string $passwd Password is also checked if specified - * @return Customer instance - */ + * Return customer instance from its e-mail (optionnaly check password) + * + * @param string $email e-mail + * @param string $passwd Password is also checked if specified + * @return Customer instance + */ public function getByEmail($email, $passwd = null, Shop $shop = null) { - if (!Validate::isEmail($email) || ($passwd && !Validate::isPasswd($passwd))) - die (Tools::displayError()); + if (!Validate::isEmail($email) || ($passwd && !Validate::isPasswd($passwd))) + die (Tools::displayError()); if (!$shop) $shop = Context::getContext()->shop; - $sql = 'SELECT * + $sql = 'SELECT * FROM `'._DB_PREFIX_.'customer` WHERE `active` = 1 AND `email` = \''.pSQL($email).'\' @@ -252,13 +252,13 @@ class CustomerCore extends ObjectModel } /** - * Check id the customer is active or not - * - * @return boolean customer validity - */ + * Check id the customer is active or not + * + * @return boolean customer validity + */ public static function isBanned($id_customer) { - if (!Validate::isUnsignedId($id_customer)) + if (!Validate::isUnsignedId($id_customer)) return true; $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT `id_customer` @@ -268,26 +268,26 @@ class CustomerCore extends ObjectModel AND `deleted` = 0'); if (isset($result['id_customer'])) return false; - return true; + return true; } /** - * Check if e-mail is already registered in database - * - * @param string $email e-mail - * @param $return_id boolean - * @param $ignore_guest boolean, to exclude guest customer - * @return Customer ID if found, false otherwise - */ + * Check if e-mail is already registered in database + * + * @param string $email e-mail + * @param $return_id boolean + * @param $ignore_guest boolean, to exclude guest customer + * @return Customer ID if found, false otherwise + */ public static function customerExists($email, $return_id = false, $ignore_guest = true, Shop $shop = null) { - if (!Validate::isEmail($email)) - die (Tools::displayError()); + if (!Validate::isEmail($email)) + die (Tools::displayError()); if (!$shop) $shop = Context::getContext()->shop; - $sql = 'SELECT `id_customer` + $sql = 'SELECT `id_customer` FROM `'._DB_PREFIX_.'customer` WHERE `email` = \''.pSQL($email).'\' '.$shop->addSqlRestriction(Shop::SHARE_CUSTOMER). @@ -300,12 +300,12 @@ class CustomerCore extends ObjectModel } /** - * Check if an address is owned by a customer - * - * @param integer $id_customer Customer ID - * @param integer $id_address Address ID - * @return boolean result - */ + * Check if an address is owned by a customer + * + * @param integer $id_customer Customer ID + * @param integer $id_address Address ID + * @return boolean result + */ public static function customerHasAddress($id_customer, $id_address) { if (!array_key_exists($id_customer, self::$_customerHasAddress)) @@ -327,11 +327,11 @@ class CustomerCore extends ObjectModel } /** - * Return customer addresses - * - * @param integer $id_lang Language ID - * @return array Addresses - */ + * Return customer addresses + * + * @param integer $id_lang Language ID + * @return array Addresses + */ public function getAddresses($id_lang) { $sql = 'SELECT a.*, cl.`name` AS country, s.name AS state, s.iso_code AS state_iso @@ -344,11 +344,11 @@ class CustomerCore extends ObjectModel } /** - * Count the number of addresses for a customer - * - * @param integer $id_customer Customer ID - * @return integer Number of addresses - */ + * Count the number of addresses for a customer + * + * @param integer $id_customer Customer ID + * @return integer Number of addresses + */ public static function getAddressesTotalById($id_customer) { return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' @@ -360,17 +360,17 @@ class CustomerCore extends ObjectModel } /** - * Check if customer password is the right one - * - * @param string $passwd Password - * @return boolean result - */ + * Check if customer password is the right one + * + * @param string $passwd Password + * @return boolean result + */ public static function checkPassword($id_customer, $passwd) { - if (!Validate::isUnsignedId($id_customer) || !Validate::isMd5($passwd)) - die (Tools::displayError()); + if (!Validate::isUnsignedId($id_customer) || !Validate::isMd5($passwd)) + die (Tools::displayError()); - $sql = 'SELECT `id_customer` + $sql = 'SELECT `id_customer` FROM `'._DB_PREFIX_.'customer` WHERE `id_customer` = '.$id_customer.' AND `passwd` = \''.$passwd.'\''; @@ -378,11 +378,11 @@ class CustomerCore extends ObjectModel } /** - * Light back office search for customers - * - * @param string $query Searched string - * @return array Corresponding customers - */ + * Light back office search for customers + * + * @param string $query Searched string + * @return array Corresponding customers + */ public static function searchByName($query, Shop $shop = null) { if (!$shop) @@ -400,10 +400,10 @@ class CustomerCore extends ObjectModel } /** - * Search for customers by ip address - * - * @param string $ip Searched string - */ + * Search for customers by ip address + * + * @param string $ip Searched string + */ public static function searchByIp($ip) { return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' @@ -415,10 +415,10 @@ class CustomerCore extends ObjectModel } /** - * Return several useful statistics about customer - * - * @return array Stats - */ + * Return several useful statistics about customer + * + * @return array Stats + */ public function getStats() { $result = Db::getInstance()->getRow(' @@ -445,15 +445,15 @@ class CustomerCore extends ObjectModel public function getLastConnections() { - return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' - SELECT c.date_add, COUNT(cp.id_page) AS pages, TIMEDIFF(MAX(cp.time_end), c.date_add) as time, http_referer,INET_NTOA(ip_address) as ipaddress - FROM `'._DB_PREFIX_.'guest` g - LEFT JOIN `'._DB_PREFIX_.'connections` c ON c.id_guest = g.id_guest - LEFT JOIN `'._DB_PREFIX_.'connections_page` cp ON c.id_connections = cp.id_connections - WHERE g.`id_customer` = '.(int)$this->id.' - GROUP BY c.`id_connections` - ORDER BY c.date_add DESC - LIMIT 10'); + return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT c.date_add, COUNT(cp.id_page) AS pages, TIMEDIFF(MAX(cp.time_end), c.date_add) as time, http_referer,INET_NTOA(ip_address) as ipaddress + FROM `'._DB_PREFIX_.'guest` g + LEFT JOIN `'._DB_PREFIX_.'connections` c ON c.id_guest = g.id_guest + LEFT JOIN `'._DB_PREFIX_.'connections_page` cp ON c.id_connections = cp.id_connections + WHERE g.`id_customer` = '.(int)$this->id.' + GROUP BY c.`id_connections` + ORDER BY c.date_add DESC + LIMIT 10'); } /* @@ -612,8 +612,8 @@ class CustomerCore extends ObjectModel $vars = array( '{firstname}' => $this->firstname, '{lastname}' => $this->lastname, - '{email}' => $this->email, - '{passwd}' => $password + '{email}' => $this->email, + '{passwd}' => $password ); Mail::Send( @@ -642,28 +642,28 @@ class CustomerCore extends ObjectModel } /** - * Check customer informations and return customer validity - * - * @since 1.5.0 - * @param boolean $with_guest - * @return boolean customer validity - */ + * Check customer informations and return customer validity + * + * @since 1.5.0 + * @param boolean $with_guest + * @return boolean customer validity + */ public function isLogged($with_guest = false) { if (!$with_guest && $this->is_guest == 1) return false; /* Customer is valid only if it can be load and if object password is the same as database one */ - if ($this->logged == 1 && $this->id && Validate::isUnsignedId($this->id) && self::checkPassword($this->id, $this->passwd)) - return true; - return false; + if ($this->logged == 1 && $this->id && Validate::isUnsignedId($this->id) && self::checkPassword($this->id, $this->passwd)) + return true; + return false; } /** - * Logout - * - * @since 1.5.0 - */ + * Logout + * + * @since 1.5.0 + */ public function logout() { if (isset(Context::getContext()->cookie)) @@ -672,11 +672,11 @@ class CustomerCore extends ObjectModel } /** - * Soft logout, delete everything links to the customer - * but leave there affiliate's informations - * - * @since 1.5.0 - */ + * Soft logout, delete everything links to the customer + * but leave there affiliate's informations + * + * @since 1.5.0 + */ public function mylogout() { if (isset(Context::getContext()->cookie)) diff --git a/controllers/front/AuthController.php b/controllers/front/AuthController.php index 8731bb44a..1fc78f706 100644 --- a/controllers/front/AuthController.php +++ b/controllers/front/AuthController.php @@ -294,17 +294,22 @@ class AuthControllerCore extends FrontController $this->context->cookie->is_guest = $customer->isGuest(); $this->context->cookie->passwd = $customer->passwd; $this->context->cookie->email = $customer->email; - if (Configuration::get('PS_CART_FOLLOWING') && (empty($this->context->cookie->id_cart) || Cart::getNbProducts($this->context->cookie->id_cart) == 0)) - $this->context->cookie->id_cart = (int)Cart::lastNoneOrderedCart($this->context->customer->id); - // Update cart address - $this->context->cart->setDeliveryOption(null); - $this->context->cart->id_address_delivery = Address::getFirstCustomerAddressId((int)($customer->id)); - $this->context->cart->id_address_invoice = Address::getFirstCustomerAddressId((int)($customer->id)); - $this->context->cart->secure_key = $customer->secure_key; - $this->context->cart->update(); // 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)) + $this->context->cookie->id_cart = (int)Cart::lastNoneOrderedCart($this->context->customer->id); + + // Update cart address + $this->context->cart->id = $this->context->cookie->id_cart; + $this->context->cart->setDeliveryOption(null); + $this->context->cart->id_address_delivery = Address::getFirstCustomerAddressId((int)($customer->id)); + + $this->context->cart->id_address_invoice = Address::getFirstCustomerAddressId((int)($customer->id)); + $this->context->cart->secure_key = $customer->secure_key; + $this->context->cart->update(); + $this->context->cart->autosetProductAddress(); Hook::exec('authentication');