![usps-logo[1]](http://e-commsolution.com/wp-content/uploads/2011/09/usps-logo1-363x288.jpg)
USPS real time rates are a feature that has been long desired, you can always pay a developer but here is a walk through. I was working on other projects and for a long time I had some issues with USPS’ API and it is because their testing server is not all that functional so after sending an email to USPS API support they put my user ID to the production server and I was getting rates! I have to give credit where credit is due and say that Mark Sanborn’s Blog helped me and gave me about 90% of the PHP code needed to get this feature working.
I have cleaned up Mark’s code to better fit the Yahoo store platform and to make it easier for YOU to add your options.
There are a few things to keep in mind with this code,
- It is in copy and paste format and will work with the Yahoo web hosting PHP.
- You will need to go to USPS site to sign up for a user name and get off the testing server (will take an email or 2).
- Ship weight will be rounded up to the nearest whole number, so 2.3 lbs will be 3 lbs for the USPS rate. I have done some testing and found out that 2.3 lbs is the same price as 3 lbs.
And without farther ado here is the code!
-
<?
-
// ========== CHANGE THESE VALUES TO MATCH YOUR OWN ===========
-
-
$userName = "USERNAME"; // Your USPS Username
-
$orgin_zip = "90210"; // location that you are shipping from.
-
$add_rate = ""; // amount that you would like to add on top of usps rate, can be left blank
-
// =============== DON’T CHANGE BELOW THIS LINE ===============
-
-
// ====== Yahoo Post Script Values ========
-
$dest_zip = $_POST[‘Ship-Zip’];
-
$weight = $_POST[‘Total-Weight’];
-
$type = $_POST[‘Shipping’];
-
-
-
$url = "http://Production.ShippingAPIs.com/ShippingAPI.dll"; // production server and not test server.
-
-
-
// This section of code is from http://www.marksanborn.net/php/calculating-usps-shipping-rates-with-php/ and is the master mind for the creation.
-
// You can donate at http://www.marksanborn.net/donate… Please do if this helps your store.
-
-
$data = "API=RateV3&XML=<RateV3Request USERID=\"$userName\"><Package ID=\"1ST\"><Service>$type</Service><ZipOrigination>$orgin_zip</ZipOrigination><ZipDestination>$dest_zip</ZipDestination><Pounds>".ceil($weight)."</Pounds><Ounces>0</Ounces><Size>REGULAR</Size><Machinable>TRUE</Machinable></Package></RateV3Request>";
-
-
-
foreach ($vals as $xml_elem) {
-
if ($xml_elem[‘type’] == ‘open’) {
-
} else {
-
$level[$xml_elem[‘level’]] = $xml_elem[‘tag’];
-
}
-
}
-
if ($xml_elem[‘type’] == ‘complete’) {
-
$start_level = 1;
-
$php_stmt = ‘$params’;
-
while($start_level < $xml_elem[‘level’]) {
-
$php_stmt .= ‘[$level['.$start_level.']]’;
-
$start_level++;
-
}
-
$php_stmt .= ‘[$xml_elem[\'tag\']] = $xml_elem[\'value\'];’;
-
}
-
}
-
$price = $params[‘RATEV3RESPONSE’][’1ST’][’1′][‘RATE’];
-
if($add_rate != ""){
-
$price = $price+$add_rate;
-
}
-
?>
With all of my code you will need to test and you can do that with the shipping and tax test in the store, you will also need to make sure that your shipping Methods in the store match that of USPS, here is the list and they ARE case-sensitive
- FIRST CLASS
- PRIORITY
- EXPRESS
- PARCEL

6 Comments
Natural
Posted October 7, 2011 at 7:02 am | PermalinkWhere would one paste this code in a yahoo store?
drifterz28
Posted October 7, 2011 at 8:31 pm | PermalinkThis code belongs as a PHP page on the webhosting side
adam75dfw
Posted January 26, 2012 at 6:24 pm | PermalinkI still don’t understand where I put this code in my store. Apparently it’s to be saved in my Yahoo host files as .php file? Is that correct? If so, where in my html or Shipping Manager do I reference the file?
If not, then how and where is the file to be saved and referenced?
I would love to make this work. Any help will be appreciated.
E-comm
Posted February 4, 2012 at 12:29 pm | Permalinkso what you will need to do is go to the “web hosting control panel” from there click on “Manage” -> “file manager” then click on “PHP” in the upper right hand corner of the page under “create” copy and paste this code and change what you need to.
If you are still having issues E-comm solution will be happy to install USPS basic script for you for $50, feel free to email info@e-commsolution.com
crazyschmiddy
Posted April 23, 2012 at 12:43 pm | PermalinkHello
Quick question do you need MS Std or MS Pro account with API access.
Thanks,
Eric
E-comm
Posted May 29, 2012 at 7:38 pm | PermalinkYes, MS Standard or Pro is needed.