try to fix missing zip error

This commit is contained in:
O K
2026-01-03 10:50:31 +02:00
parent cdb9341a4f
commit 76d5544cbc

View File

@@ -153,7 +153,7 @@ class Usps_Api_Bridge extends Module
return $helper->generateForm([$fields_form]); 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'); require_once(dirname(__FILE__) . '/classes/UspsV3Client.php');
@@ -161,9 +161,7 @@ public function calculateRate($params, $shipping_cost, $products, $originalModul
$token = $this->getAccessToken(); $token = $this->getAccessToken();
if (!$token) return false; if (!$token) return false;
// 2. Identify Service (Module Loop context)
$carrierId = (int)$originalModule->id_carrier; $carrierId = (int)$originalModule->id_carrier;
// Fallback
if (!$carrierId && isset($params->id_carrier)) { if (!$carrierId && isset($params->id_carrier)) {
$carrierId = (int)$params->id_carrier; $carrierId = (int)$params->id_carrier;
} }
@@ -171,7 +169,6 @@ public function calculateRate($params, $shipping_cost, $products, $originalModul
// 3. Get Method Code // 3. Get Method Code
$sql = 'SELECT code FROM `' . _DB_PREFIX_ . 'uspsl_method` WHERE id_carrier = ' . (int)$carrierId; $sql = 'SELECT code FROM `' . _DB_PREFIX_ . 'uspsl_method` WHERE id_carrier = ' . (int)$carrierId;
$methodCode = \Db::getInstance()->getValue($sql); $methodCode = \Db::getInstance()->getValue($sql);
if (!$methodCode) return false; if (!$methodCode) return false;
// --- 4. CHECK LEGACY DB CACHE --- // --- 4. CHECK LEGACY DB CACHE ---
@@ -180,14 +177,11 @@ public function calculateRate($params, $shipping_cost, $products, $originalModul
if ($canCache) { if ($canCache) {
$zhCache = \UspsPsLabels\Cache::cacheCart($params->id); $zhCache = \UspsPsLabels\Cache::cacheCart($params->id);
if (\Validate::isLoadedObject($zhCache)) { if (\Validate::isLoadedObject($zhCache)) {
$sql = 'SELECT rate FROM `' . _DB_PREFIX_ . 'uspsl_cache_rate` $sql = 'SELECT rate FROM `' . _DB_PREFIX_ . 'uspsl_cache_rate`
WHERE id_cache = ' . (int)$zhCache->id . ' WHERE id_cache = ' . (int)$zhCache->id . '
AND id_carrier = ' . (int)$carrierId; AND id_carrier = ' . (int)$carrierId;
$cachedRate = \Db::getInstance()->getValue($sql); $cachedRate = \Db::getInstance()->getValue($sql);
if ($cachedRate !== false && $cachedRate !== null) { if ($cachedRate !== false && $cachedRate !== null) {
return (float)$cachedRate + $shipping_cost; 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) // 5. Determine International Status & Address Data (Cookie/Object Hybrid)
$destAddress = new \Address($params->id_address_delivery);
$destZip = ''; $destZip = '';
$destCountryIso = 'US'; $destCountryIso = '';
if (\Validate::isLoadedObject($destAddress)) { if (!empty($params->id_address_delivery)) {
// Real Address exists $address = new Address($params->id_address_delivery);
$destZip = $destAddress->postcode; if (Validate::isLoadedObject($address)) {
$destCountryIso = \Country::getIsoById($destAddress->id_country); $destZip = $address->postcode;
} else { $destCountryIso = Country::getIsoById($address->id_country);
// 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 (isset($context->cookie->postcode) && $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; $destZip = $context->cookie->postcode;
} }
if (empty($destCountryIso) && isset($context->cookie->id_country)) {
// If absolutely no data, we can't calculate $destCountryIso = Country::getIsoById((int)$context->cookie->id_country);
if (empty($destZip) && empty($destCountryIso)) { } else if (empty($destCountryIso) && isset($params->id_country)) {
return false; $destCountryIso = Country::getIsoById((int)$params->id_country);
} }
if (empty($destCountryIso)) {
$destCountryIso = 'US';
}
if (empty($destZip)) {
return false;
} }
// Clean Data // Clean Data
@@ -281,7 +282,6 @@ public function calculateRate($params, $shipping_cost, $products, $originalModul
$payload['rateIndicator'] = $flatRateIndicator; $payload['rateIndicator'] = $flatRateIndicator;
} }
// FIX: Pass destCountryIso directly, do not rely on Address object inside this helper
$response = $this->sendApiRequest($client, $payload, $isInternational, $destCountryIso, $destZip); $response = $this->sendApiRequest($client, $payload, $isInternational, $destCountryIso, $destZip);
if (isset($response['error']) && $payload['priceType'] === 'COMMERCIAL') { if (isset($response['error']) && $payload['priceType'] === 'COMMERCIAL') {