From 8d912198abcd8825e136b68e8fcb54b42a299eeb Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Mon, 21 Aug 2023 16:32:06 +0530 Subject: [PATCH 1/3] Fix address update issue at front office --- controllers/front/AddressController.php | 76 +++++++++++++------------ 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/controllers/front/AddressController.php b/controllers/front/AddressController.php index 3fd2107ee..7f97f869b 100644 --- a/controllers/front/AddressController.php +++ b/controllers/front/AddressController.php @@ -175,43 +175,47 @@ class AddressControllerCore extends FrontController } } - // Save address - if ($result = $address->save()) { - // Update id address of the current cart if necessary - if (isset($address_old) && $address_old->isUsed()) { - $this->context->cart->updateAddressId($address_old->id, $address->id); - } else { // Update cart address - $this->context->cart->autosetProductAddress(); - } - - if ((bool)Tools::getValue('select_address', false) == true || (Tools::getValue('type') == 'invoice' && Configuration::get('PS_ORDER_PROCESS_TYPE'))) { - $this->context->cart->id_address_invoice = (int)$address->id; - } elseif (Configuration::get('PS_ORDER_PROCESS_TYPE')) { - $this->context->cart->id_address_invoice = (int)$this->context->cart->id_address_delivery; - } - $this->context->cart->update(); - - if ($this->ajax) { - $return = array( - 'hasError' => (bool)$this->errors, - 'errors' => $this->errors, - 'id_address_delivery' => (int)$this->context->cart->id_address_delivery, - 'id_address_invoice' => (int)$this->context->cart->id_address_invoice - ); - $this->ajaxDie(json_encode($return)); - } - - // Redirect to old page or current page - if ($back = Tools::getValue('back')) { - if ($back == Tools::secureReferrer(Tools::getValue('back'))) { - Tools::redirect(html_entity_decode($back)); - } - $mod = Tools::getValue('mod'); - Tools::redirect('index.php?controller='.$back.($mod ? '&back='.$mod : '')); - } else { - Tools::redirect('index.php?controller=address&updated=1'); - } + // Update address if customer already has address + if ($idAddress = Customer::getCustomerIdAddress($address->id_customer)) { + $address->id = $idAddress; } + $address->save(); + + // Update id address of the current cart if necessary + if (isset($address_old) && $address_old->isUsed()) { + $this->context->cart->updateAddressId($address_old->id, $address->id); + } else { // Update cart address + $this->context->cart->autosetProductAddress(); + } + + if ((bool)Tools::getValue('select_address', false) == true || (Tools::getValue('type') == 'invoice' && Configuration::get('PS_ORDER_PROCESS_TYPE'))) { + $this->context->cart->id_address_invoice = (int)$address->id; + } elseif (Configuration::get('PS_ORDER_PROCESS_TYPE')) { + $this->context->cart->id_address_invoice = (int)$this->context->cart->id_address_delivery; + } + $this->context->cart->update(); + + if ($this->ajax) { + $return = array( + 'hasError' => (bool)$this->errors, + 'errors' => $this->errors, + 'id_address_delivery' => (int)$this->context->cart->id_address_delivery, + 'id_address_invoice' => (int)$this->context->cart->id_address_invoice + ); + $this->ajaxDie(json_encode($return)); + } + + // Redirect to old page or current page + if ($back = Tools::getValue('back')) { + if ($back == Tools::secureReferrer(Tools::getValue('back'))) { + Tools::redirect(html_entity_decode($back)); + } + $mod = Tools::getValue('mod'); + Tools::redirect('index.php?controller='.$back.($mod ? '&back='.$mod : '')); + } else { + Tools::redirect('index.php?controller=address&updated=1'); + } + $this->errors[] = Tools::displayError('An error occurred while updating your address.'); } From 7f65a164e1a0ee55218618eee6243cfc1eff5386 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Wed, 22 Nov 2023 12:49:17 +0530 Subject: [PATCH 2/3] Revert "Fix address update issue at front office" This reverts commit 8d912198abcd8825e136b68e8fcb54b42a299eeb. --- controllers/front/AddressController.php | 74 ++++++++++++------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/controllers/front/AddressController.php b/controllers/front/AddressController.php index 7f97f869b..3fd2107ee 100644 --- a/controllers/front/AddressController.php +++ b/controllers/front/AddressController.php @@ -175,47 +175,43 @@ class AddressControllerCore extends FrontController } } - // Update address if customer already has address - if ($idAddress = Customer::getCustomerIdAddress($address->id_customer)) { - $address->id = $idAddress; - } - $address->save(); - - // Update id address of the current cart if necessary - if (isset($address_old) && $address_old->isUsed()) { - $this->context->cart->updateAddressId($address_old->id, $address->id); - } else { // Update cart address - $this->context->cart->autosetProductAddress(); - } - - if ((bool)Tools::getValue('select_address', false) == true || (Tools::getValue('type') == 'invoice' && Configuration::get('PS_ORDER_PROCESS_TYPE'))) { - $this->context->cart->id_address_invoice = (int)$address->id; - } elseif (Configuration::get('PS_ORDER_PROCESS_TYPE')) { - $this->context->cart->id_address_invoice = (int)$this->context->cart->id_address_delivery; - } - $this->context->cart->update(); - - if ($this->ajax) { - $return = array( - 'hasError' => (bool)$this->errors, - 'errors' => $this->errors, - 'id_address_delivery' => (int)$this->context->cart->id_address_delivery, - 'id_address_invoice' => (int)$this->context->cart->id_address_invoice - ); - $this->ajaxDie(json_encode($return)); - } - - // Redirect to old page or current page - if ($back = Tools::getValue('back')) { - if ($back == Tools::secureReferrer(Tools::getValue('back'))) { - Tools::redirect(html_entity_decode($back)); + // Save address + if ($result = $address->save()) { + // Update id address of the current cart if necessary + if (isset($address_old) && $address_old->isUsed()) { + $this->context->cart->updateAddressId($address_old->id, $address->id); + } else { // Update cart address + $this->context->cart->autosetProductAddress(); } - $mod = Tools::getValue('mod'); - Tools::redirect('index.php?controller='.$back.($mod ? '&back='.$mod : '')); - } else { - Tools::redirect('index.php?controller=address&updated=1'); - } + if ((bool)Tools::getValue('select_address', false) == true || (Tools::getValue('type') == 'invoice' && Configuration::get('PS_ORDER_PROCESS_TYPE'))) { + $this->context->cart->id_address_invoice = (int)$address->id; + } elseif (Configuration::get('PS_ORDER_PROCESS_TYPE')) { + $this->context->cart->id_address_invoice = (int)$this->context->cart->id_address_delivery; + } + $this->context->cart->update(); + + if ($this->ajax) { + $return = array( + 'hasError' => (bool)$this->errors, + 'errors' => $this->errors, + 'id_address_delivery' => (int)$this->context->cart->id_address_delivery, + 'id_address_invoice' => (int)$this->context->cart->id_address_invoice + ); + $this->ajaxDie(json_encode($return)); + } + + // Redirect to old page or current page + if ($back = Tools::getValue('back')) { + if ($back == Tools::secureReferrer(Tools::getValue('back'))) { + Tools::redirect(html_entity_decode($back)); + } + $mod = Tools::getValue('mod'); + Tools::redirect('index.php?controller='.$back.($mod ? '&back='.$mod : '')); + } else { + Tools::redirect('index.php?controller=address&updated=1'); + } + } $this->errors[] = Tools::displayError('An error occurred while updating your address.'); } From 2648cc728b9bbd2a4714004d4b0a8044f684aa2c Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Wed, 22 Nov 2023 12:51:41 +0530 Subject: [PATCH 3/3] Fix address issue --- classes/Address.php | 2 +- classes/Customer.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/classes/Address.php b/classes/Address.php index afd5df5ba..53a759500 100644 --- a/classes/Address.php +++ b/classes/Address.php @@ -180,7 +180,7 @@ class AddressCore extends ObjectModel { // for customer address we need to check of customer address already exists. if ($this->id_customer) { - if ($id_address = Customer::getCustomerIdAddress($this->id_customer)) { + if ($id_address = Customer::getCustomerIdAddress($this->id_customer, false)) { return false; } } diff --git a/classes/Customer.php b/classes/Customer.php index 718c47161..e2d529aa1 100644 --- a/classes/Customer.php +++ b/classes/Customer.php @@ -465,10 +465,10 @@ class CustomerCore extends ObjectModel return Cache::retrieve($cache_id); } - public static function getCustomerIdAddress($id_customer) + public static function getCustomerIdAddress($id_customer, $use_cache = true) { $cache_id = 'Customer::getCustomerIdAddress'.(int)$id_customer; - if (!Cache::isStored($cache_id)) { + if (!$use_cache || !Cache::isStored($cache_id)) { $sql = 'SELECT id_address FROM `'._DB_PREFIX_.'address` a WHERE `id_customer` = '.(int)$id_customer.' AND a.`deleted` = 0';