From 6e0f2e7d22438e1570ac5d496286822137286076 Mon Sep 17 00:00:00 2001 From: Mrakor <20678997+Mrakor@users.noreply.github.com> Date: Thu, 28 Mar 2024 09:31:25 +0100 Subject: [PATCH 1/2] PES-2028 Remove 'alzabox' from vendor groups in saved shipment methods during plugin update --- install.zasilkovna.php | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/install.zasilkovna.php b/install.zasilkovna.php index d9513fc..7d5c83b 100644 --- a/install.zasilkovna.php +++ b/install.zasilkovna.php @@ -104,6 +104,7 @@ public function preflight($route, $adapter) { recurse_delete($media_path); $this->removeAdministratorFiles(); + $this->removeAlzaboxFromShipmentParams(); } } @@ -598,5 +599,53 @@ private function createCronToken() { $model->updateConfig($config); } + + /** + * @return void + */ + private function removeAlzaboxFromShipmentParams() { + $db = JFactory::getDBO(); + $db->setQuery('SELECT `virtuemart_shipmentmethod_id`, `shipment_params` FROM `#__virtuemart_shipmentmethods` WHERE `shipment_element` = "zasilkovna" AND `shipment_params` LIKE "%alzabox%"'); + $methods = $db->loadObjectList(); + + foreach ($methods as $method) { + $newParams = $this->removeAlzaboxFromShipmentParamsString($method->shipment_params); + $updateQuery = sprintf( + 'UPDATE `#__virtuemart_shipmentmethods` SET `shipment_params` = %s WHERE `virtuemart_shipmentmethod_id` = %d', + $db->quote($newParams), + (int)$method->virtuemart_shipmentmethod_id + ); + + $db->setQuery($updateQuery); + $db->execute(); + } + } + + /** + * @param string $shipmentParamsString + * @return string + */ + private function removeAlzaboxFromShipmentParamsString($shipmentParamsString) { + // Split the string into JSON part and the rest + list($jsonPart, $rest) = explode("|", $shipmentParamsString, 2); + + // Remove 'delivery_settings=' from the start of the JSON part + $jsonPart = str_replace('delivery_settings=', '', $jsonPart); + + // Check if the string contains "alzabox" + if (strpos($jsonPart, 'alzabox') !== false) { + $arr = json_decode($jsonPart, true); + + // Remove "alzabox" from the vendor_groups array + if (($key = array_search('alzabox', $arr['vendor_groups'], true)) !== false) { + unset($arr['vendor_groups'][$key]); + $arr['vendor_groups'] = array_values($arr['vendor_groups']); + } + + $jsonPart = json_encode($arr); + } + + return 'delivery_settings=' . $jsonPart . "|" . $rest; + } } From 4f5c4c92d4c79fb2470afc6d93186bad07e765b4 Mon Sep 17 00:00:00 2001 From: Mrakor <20678997+Mrakor@users.noreply.github.com> Date: Thu, 28 Mar 2024 10:06:39 +0100 Subject: [PATCH 2/2] PES-2028 Remove payment-shipment restrictions from config during update --- install.zasilkovna.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/install.zasilkovna.php b/install.zasilkovna.php index 7d5c83b..72b45b7 100644 --- a/install.zasilkovna.php +++ b/install.zasilkovna.php @@ -102,7 +102,7 @@ public function preflight($route, $adapter) { if ($route === 'update') { $media_path = JPATH_ROOT . DS . 'media' . DS . 'com_zasilkovna'; recurse_delete($media_path); - + $this->removeObsoleteShippingPaymentRestrictionsFromConfig(); $this->removeAdministratorFiles(); $this->removeAlzaboxFromShipmentParams(); } @@ -647,5 +647,18 @@ private function removeAlzaboxFromShipmentParamsString($shipmentParamsString) { return 'delivery_settings=' . $jsonPart . "|" . $rest; } + + private function removeObsoleteShippingPaymentRestrictionsFromConfig() { + /** @var VirtueMartModelZasilkovna $zasilkovnaModel */ + $zasilkovnaModel = VmModel::getModel('zasilkovna'); + $config = $zasilkovnaModel->loadConfig(); + + foreach ($config as $key => $value) { + if (strpos($key, 'zasilkovna_combination') === 0) { + unset($config[$key]); + } + } + $zasilkovnaModel->updateConfig($config); + } }