Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 551095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:21:27+00:00 2026-05-13T11:21:27+00:00

I have managed to get a UPS Shipping calculator working in PHP. I now

  • 0

I have managed to get a UPS Shipping calculator working in PHP. I now found out that the standard shipping from UPS is capped at 150lbs. For anything over 150lbs, I need to ship via freight.

Has anyone ever coded a Freight Calculator with UPS? The calculator I wrote for anything under 150lbs does not require a UPS account. I was hoping to do the same for the freight calculator, however, I am unable to find any information on how to accomplish this.

Any help will be greatly appreciated.

EDIT

As per request by Nicolaesse, I have added some code snippets that I wrote in 2009 to solve my problem.

Demo can be seen here:
http://cart.jgallant.com (Add product to cart, checkout, and enter zipcode for shipping estimate)

Core UPS class:

<?php
    class ups {

    function getMethod() {
        $method= array(
                '1DA'    => 'Next Day Air',
                '2DA'    => '2nd Day Air',
                '3DS'    => '3 Day Select',
                'GND'    => 'Ground',
            );
        return $method;
    }

    function get_item_shipping($weight, $zipcode)
    {
        unset($_SESSION['zipcode_error']);
        if($weight > 150) {
             $_SESSION['zipcode_error'] = "A cart item requires freight.";
        }
        if (!isset($_SESSION['zipcode_error'])) {
            $services = $this->getMethod();
            $ch = curl_init();
            foreach ($services as $key => $service) {
                $Url = join("&", array("http://www.ups.com/using/services/rave/qcostcgi.cgi?accept_UPS_license_agreement=yes",
                "10_action=3",
                "13_product=".$key,
                "14_origCountry=US",
                "15_origPostal=90210",
                "19_destPostal=" . $zipcode,
                "22_destCountry=US",
                "23_weight=" . $weight,
                "47_rate_chart=Regular+Daily+Pickup",
                "48_container=00",
                "49_residential=2",
                "billToUPS=no")
                );

                    curl_setopt($ch, CURLOPT_URL, $Url);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    $Results[]=curl_exec($ch);
            }
            curl_close($ch);
            $pre = "";
            $shipping_list = array();
            foreach($Results as $result) {
                $result = explode("%", $result);
                if ($services[$result[1]] != ''){
                    if ((($result[1]=='XPR') && ($pre == 'XPR')) || (($result[1]=='XDM') && ($pre == 'XDM')) || (($result[1]=='1DP') && ($pre == '1DP')) || (($result[1]=='1DM') && ($pre == '1DM')) || (($result[1]=='1DA') && ($pre == '1DA')) || (($result[1]=='2DA') && ($pre == '2DA')))
                        $shipping_list += array($services[$result[1]."L"] => $result[8]);
                    else if (($result[1]=='GND') && ($pre == 'GND'))
                        $shipping_list += array($services[$result[1]."RES"] => $result[8]);
                    else
                        $shipping_list += array($services[$result[1]] => $result[8]);
                    $pre = $result[1];
                }
            }
        }
        $shipping_list = array_reverse($shipping_list);
        return $shipping_list;
    }

}


?>

WEIGHT Calculator Helper:

<?php

    //UPS calculator - Divides packages into 150lbs max packages, and requests multiple quotes if needed
    //Returns a sum of all the quotes added together.  
    private function calculate_per_item_shipping() {

        $currWeight = 0;
        $packageCount = 0;

        //Loop thru cart items - adding weights to packages
        foreach($this->get_contents() as $item) { 
            for ($i = 0; $i < $item["qty"]; $i++) {
                $itemWeight = $item["weight"];

                if ($itemWeight > 150) {  // A single item cannot be more than 150lbs
                  $_SESSION['zipcode_error'] = $item['name'] . " weighs more than 150lbs.";
                  return false;
                }
                else {
                  $currWeight += $itemWeight;           //Add item weight to active package
                  if ($currWeight > 150)  {             //Max weight reached for active package
                    $currWeight -= $itemWeight;         //Remove item from current package, too heavy
                    $loopPack = 0;                                  
                    $itemUsed = false;

                    //Check if an existing package can take the item
                    while (($loopPack != $packageCount) or ($itemUsed = false)) {
                      if ($packages[$loopPack] + $itemWeight < 150) {
                        $packages[$loopPack] += $itemWeight;
                        $itemUsed = true;
                      }
                      $loopPack++;
                    }

                    //if the item didn't fit in an existing package, create a new package for it
                    if ($itemUsed == false) {
                      $packageCount++;
                      $packages[$packageCount-1] = $currWeight;
                      $currWeight = $item["weight"];        //Put unused item back in active package            
                    }
                  }
                }
            }
        }
        //The remainder becomes a package
        $packageCount++;
        $packages[$packageCount-1] = $currWeight;

        for ($i = 0; $i < $packageCount; $i++) {
            $temptotal = $this->calc_package_shipping($packages[$i]);
            if ($temptotal['Ground'] != 0) { $total['Ground'] += $temptotal['Ground']; }
            if ($temptotal['3 Day Select'] != 0) { $total['3 Day Select'] += $temptotal['3 Day Select']; }
            if ($temptotal['2nd Day Air'] != 0) { $total['2nd Day Air'] += $temptotal['2nd Day Air']; }
            if ($temptotal['Next Day Air Saver'] != 0) { $total['Next Day Air Saver'] += $temptotal['Next Day Air Saver']; }
            if ($temptotal['Next Day Air Early AM'] != 0) { $total['Next Day Air Early AM'] += $temptotal['Next Day Air Early AM']; }
            if ($temptotal['Next Day Air'] != 0) { $total['Next Day Air'] += $temptotal['Next Day Air']; }
        }

        $this->selectedQuoteAmount = $total['Ground'];

        return $total;
    }



    private function calc_package_shipping($weight) {
        $ups = new ups();
        $shipping = $ups->get_item_shipping($weight, $this->zipcode);
        return $shipping;
    }



    ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:21:27+00:00Added an answer on May 13, 2026 at 11:21 am

    I ended up adding multiple quotes together – So a shipment that was between 150 lbs – 300 lbs would be split into two packages, and the two rate quotes would be added together to form the total rate cost.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have managed to get the following working: var transactions = from t in
I asked before about pixel-pushing, and have now managed to get far enough to
I have managed to get my first array working but no matter how many
I have managed to get my email problem sorted so now everytime a case
I have managed to get my UIView to animate when moving its frame.origin.x from
I have managed to get in app email working in my app but am
Have you managed to get Aptana Studio debugging to work? I tried following this,
I've deconstructed a standard WPF button using Blend and have managed to create a
I have a managed dll that calls into a native library. This native library
I have a managed Windows application that loads a managed C++ component that uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.