first commit

This commit is contained in:
O K
2025-12-07 12:42:55 +02:00
commit c78ec341e6
3 changed files with 580 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class Zh_UspsLabelsOverride extends Zh_UspsLabels
{
/**
* Intercept the rate calculation call.
*/
public function getPackageShippingCost($params, $shipping_cost, $products)
{
// 1. Check if Bridge Module exists and is active
/** @var Usps_Api_Bridge $bridge */
$bridge = Module::getInstanceByName('usps_api_bridge');
if ($bridge && $bridge->active) {
// 2. Check Debug IP Logic
// If configured, only these IPs use the new API.
// Everyone else continues using the old logic (parent).
if ($bridge->isIpAllowed()) {
// 3. Attempt to calculate rate via Bridge
// We pass '$this' (the original module instance) to access its config settings
$newRate = $bridge->calculateRate($params, $shipping_cost, $products, $this);
// If Bridge returns a valid numeric rate, use it.
// If it returns FALSE (api error, no token, etc), fall back to old logic.
if ($newRate !== false && $newRate !== null) {
return $newRate;
}
$bridge->log("Bridge returned false/null. Falling back to Legacy API.");
}
}
// 4. Fallback to Legacy Logic
return parent::getPackageShippingCost($params, $shipping_cost, $products);
}
/**
* Intercept the "Check API" button in Back Office
*/
public function ajaxProcessCheckApiConnection()
{
/** @var Usps_Api_Bridge $bridge */
$bridge = Module::getInstanceByName('usps_api_bridge');
if ($bridge && $bridge->active && $bridge->isIpAllowed()) {
// We can implement a specific test function in the bridge later
// For now, we just let it connect to OAuth
// $bridge->testApiConnection();
// return;
}
parent::ajaxProcessCheckApiConnection();
}
}