From 2d85ca45a9ef819aa83d0401eb7772ef777251f3 Mon Sep 17 00:00:00 2001 From: "vishalsingh.qlo325" Date: Mon, 12 May 2025 17:06:41 +0530 Subject: [PATCH] Updated and optmized the code for address updation from the order detail page --- classes/Address.php | 15 ++--------- .../admin/AdminAddressesController.php | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/classes/Address.php b/classes/Address.php index 1510cff0d..b6b69905b 100644 --- a/classes/Address.php +++ b/classes/Address.php @@ -214,19 +214,8 @@ class AddressCore extends ObjectModel } // If address is already used in any order, it will not be edited directly, set address to "deleted" and create a new address - if ($usedCount = $this->isUsed()) { - if ($this->deleted == 0) { - return $this->updateUsedAddress($this); - } else if ($usedCount >= 2 - && ($idOrder = Tools::getValue('id_order')) // if address is updated from the order detail page - && (Validate::isLoadedObject($objOrder = new Order((int) $idOrder))) - ) { - $objAddress = clone $this; - $objAddress->deleted = 1; - $objAddress->add(); - - return $objAddress; - } + if ($this->isUsed() && $this->deleted == 0) { + return $this->updateUsedAddress($this); } return parent::update($null_values); diff --git a/controllers/admin/AdminAddressesController.php b/controllers/admin/AdminAddressesController.php index 9d7d5ed47..1f9af2bac 100644 --- a/controllers/admin/AdminAddressesController.php +++ b/controllers/admin/AdminAddressesController.php @@ -458,8 +458,22 @@ class AdminAddressesControllerCore extends AdminController if ((int)Tools::getValue('id_order')) { $this->_redirect = false; // set deleted=1 as customer can have only one address and this address is for an order only - if (!$address->isUsed() || !$idAddress) { - $_POST['deleted'] = 1; + $_POST['deleted'] = 1; + // Clearing the cache for customer address. + $cache_id = 'Address::getFirstCustomerAddressId_'.(int) $this->id_customer.'-'.(bool)true; + Cache::clean($cache_id); + // if the deleted address is the customer current address then we will create a new one using the same information. + if (($customerAddressId = Address::getFirstCustomerAddressId((int) $address->id_customer)) + && $customerAddressId == $address->id + ) { + // initialized with the old address information + $objAddress = new Address($address->id); + $objAddress->id = $objAddress->id_address = null; + } + + if ($address->isUsed() > 1) { + // If this address is used in other orders, we will create a new address, and will not update the older addresses. + $this->id_object = null; } } @@ -469,10 +483,10 @@ class AdminAddressesControllerCore extends AdminController $return = false; if (empty($this->errors)) { - if (($return = parent::processSave()) - && $return->id != $this->object->id - ) { - $this->object = new Address($return->id); + $return = parent::processSave(); + // creating new address here since the customer older address needs to be set as deleted first as customer can only have one active address. + if (isset($objAddress)) { + $objAddress->save(); } } else { // if we have errors, we stay on the form instead of going back to the list