PHP code to calculate USPS retail pricing for Priority and Ground Advantage packages.
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Skylar Ittner fe10b4cf12 Add second example 11 months ago
README.md Add second example 11 months ago
RetailPriceChart.lib.php Init 11 months ago

README.md

Get USPS retail prices for a package.

Included price charts valid as of July 9, 2023.

Example Use

This example assumes an EasyPost Shipment object named $shipment and a Rate object named $rate.

require_once "RetailPriceChart.lib.php";
$retail_rate = RetailPriceChart::getPrice(
    $shipment->parcel->weight,
    $shipment->usps_zone,
    $rate->service,
    [$shipment->parcel->length, $shipment->parcel->width, $shipment->parcel->height],
    true);

More complete example for getting the retail rate from EasyPost, then falling back to this tool if possible:

$retail_rate = $rate->retail_rate ?? ($rate->list_rate ?? $rate->rate);
if ($rate->carrier == "USPS" && ($rate->retail_rate == $rate->rate) && ($rate->service == "Priority" || $rate->service == "GroundAdvantage")) {
    // EasyPost not providing retail rate, let's look it up ourselves
    if ($shipment->parcel->predefined_package == null) {
        try {
            $retail_rate = RetailPriceChart::getPrice($shipment->parcel->weight, $shipment->usps_zone, $rate->service,
                            [$shipment->parcel->length, $shipment->parcel->width, $shipment->parcel->height], true);
        } catch (Exception $ex) {
            
        }
    }
}

Documentation

function getPrice($weight, $zone, $service = "GroundAdvantage", $dimensions = [4, 6, 1], $rectangular = true)

Weight is in ounces.
Zone is a USPS zone number (regex: [1-9]).
Service is Priority or GroundAdvantage.
Dimensions are in inches.
Rectangular affects the dimensional weight calculation. Some rates will be lower for non-rectangular packages.

License

Copyright 2023 Netsyms Technologies

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Price data from the United States Postal Service as published in Notice 123, effective July 9, 2023. Purely factual data, such as the price of a service, is not a creative work. It cannot be copyrighted and is therefore in the public domain (Baker v. Selden, 1879).