Skip to content

Commit

Permalink
Improve USPS rates by not returning duplicate services (that have the…
Browse files Browse the repository at this point in the history
… same price, just different description)
  • Loading branch information
engram-design committed Aug 24, 2024
1 parent 0b16e65 commit b62dd51
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/carriers/USPS.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,29 @@ public function getRates(Shipment $shipment): ?RateResponse
});

$rates = [];
$processedRates = [];

foreach (Arr::get($data, 'rateOptions', []) as $shippingRate) {
$serviceCode = Arr::get($shippingRate, 'rates.0.mailClass', '');
$serviceName = Arr::get($shippingRate, 'rates.0.description', '');
$rate = Arr::get($shippingRate, 'rates.0.price', 0);

// We get duplicate rates (the same `price` and `mailClass`) but different description. Skip them.
$rateKey = $rate . '_' . $serviceCode;

if (isset($processedRates[$rateKey])) {
continue;
}

$rates[] = new Rate([
'carrier' => $this,
'response' => $shippingRate,
'serviceName' => $serviceName,
'serviceCode' => $serviceCode,
'rate' => $rate,
]);

$processedRates[$rateKey] = true;
}

return new RateResponse([
Expand Down

0 comments on commit b62dd51

Please sign in to comment.