From 76d5544cbcbcbfa5224ab3831c6d4c772431532a Mon Sep 17 00:00:00 2001 From: O K Date: Sat, 3 Jan 2026 10:50:31 +0200 Subject: [PATCH] try to fix missing zip error --- usps_api_bridge.php | 58 ++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/usps_api_bridge.php b/usps_api_bridge.php index a422ac6..ec74a7b 100644 --- a/usps_api_bridge.php +++ b/usps_api_bridge.php @@ -153,7 +153,7 @@ class Usps_Api_Bridge extends Module return $helper->generateForm([$fields_form]); } -public function calculateRate($params, $shipping_cost, $products, $originalModule) + public function calculateRate($params, $shipping_cost, $products, $originalModule) { require_once(dirname(__FILE__) . '/classes/UspsV3Client.php'); @@ -161,9 +161,7 @@ public function calculateRate($params, $shipping_cost, $products, $originalModul $token = $this->getAccessToken(); if (!$token) return false; - // 2. Identify Service (Module Loop context) $carrierId = (int)$originalModule->id_carrier; - // Fallback if (!$carrierId && isset($params->id_carrier)) { $carrierId = (int)$params->id_carrier; } @@ -171,7 +169,6 @@ public function calculateRate($params, $shipping_cost, $products, $originalModul // 3. Get Method Code $sql = 'SELECT code FROM `' . _DB_PREFIX_ . 'uspsl_method` WHERE id_carrier = ' . (int)$carrierId; $methodCode = \Db::getInstance()->getValue($sql); - if (!$methodCode) return false; // --- 4. CHECK LEGACY DB CACHE --- @@ -180,14 +177,11 @@ public function calculateRate($params, $shipping_cost, $products, $originalModul if ($canCache) { $zhCache = \UspsPsLabels\Cache::cacheCart($params->id); - if (\Validate::isLoadedObject($zhCache)) { $sql = 'SELECT rate FROM `' . _DB_PREFIX_ . 'uspsl_cache_rate` WHERE id_cache = ' . (int)$zhCache->id . ' AND id_carrier = ' . (int)$carrierId; - $cachedRate = \Db::getInstance()->getValue($sql); - if ($cachedRate !== false && $cachedRate !== null) { return (float)$cachedRate + $shipping_cost; } @@ -196,32 +190,39 @@ public function calculateRate($params, $shipping_cost, $products, $originalModul // ------------------------------- // 5. Determine International Status & Address Data (Cookie/Object Hybrid) - $destAddress = new \Address($params->id_address_delivery); $destZip = ''; - $destCountryIso = 'US'; + $destCountryIso = ''; - if (\Validate::isLoadedObject($destAddress)) { - // Real Address exists - $destZip = $destAddress->postcode; - $destCountryIso = \Country::getIsoById($destAddress->id_country); - } else { - // Fallback for Estimator (Cookies) - $context = \Context::getContext(); - - if (isset($context->cookie->id_country) && $context->cookie->id_country) { - $destCountryIso = \Country::getIsoById($context->cookie->id_country); - } elseif (isset($params->id_country) && $params->id_country) { - $destCountryIso = \Country::getIsoById($params->id_country); + if (!empty($params->id_address_delivery)) { + $address = new Address($params->id_address_delivery); + if (Validate::isLoadedObject($address)) { + $destZip = $address->postcode; + $destCountryIso = Country::getIsoById($address->id_country); } + } - if (isset($context->cookie->postcode) && $context->cookie->postcode) { - $destZip = $context->cookie->postcode; - } + if (empty($destZip) && Tools::getIsset('postcode')) { + $destZip = Tools::getValue('postcode'); + } + if (empty($destCountryIso) && Tools::getIsset('id_country')) { + $destCountryIso = Country::getIsoById((int)Tools::getValue('id_country')); + } + $context = Context::getContext(); + if (empty($destZip) && isset($context->cookie->postcode)) { + $destZip = $context->cookie->postcode; + } + if (empty($destCountryIso) && isset($context->cookie->id_country)) { + $destCountryIso = Country::getIsoById((int)$context->cookie->id_country); + } else if (empty($destCountryIso) && isset($params->id_country)) { + $destCountryIso = Country::getIsoById((int)$params->id_country); + } - // If absolutely no data, we can't calculate - if (empty($destZip) && empty($destCountryIso)) { - return false; - } + if (empty($destCountryIso)) { + $destCountryIso = 'US'; + } + + if (empty($destZip)) { + return false; } // Clean Data @@ -281,7 +282,6 @@ public function calculateRate($params, $shipping_cost, $products, $originalModul $payload['rateIndicator'] = $flatRateIndicator; } - // FIX: Pass destCountryIso directly, do not rely on Address object inside this helper $response = $this->sendApiRequest($client, $payload, $isInternational, $destCountryIso, $destZip); if (isset($response['error']) && $payload['priceType'] === 'COMMERCIAL') {