Yahoo Store Real Time Shipping with USPS
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’];
header("HTTP/1.0 200 OK"); //response the yahoo store needs to confirm
$url = "http://Production.ShippingAPIs.com/ShippingAPI.dll"; // production server and not test server.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
// 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>";
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$result=curl_exec ($ch);
$data = strstr($result, ‘<?’);
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);
$params = array();
$level = array();
foreach ($vals as $xml_elem) {
if ($xml_elem[‘type’] == ‘open’) {
if (array_key_exists(‘attributes’,$xml_elem)) {
list($level[$xml_elem[‘level’]],$extra) = array_values($xml_elem[‘attributes’]);
} 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\'];’;
eval($php_stmt);
}
}
curl_close($ch);
$price = $params[‘RATEV3RESPONSE’][’1ST’][’1′][‘RATE’];
if($add_rate != ""){
$price = $price+$add_rate;
}
header("Shipping-Charge: $price");
?>
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
4 Comments
Other Links to this Post
RSS feed for comments on this post.
Leave a comment
You must be logged in to post a comment.
By Natural
, October 7, 2011 @ 7:02 am
Where would one paste this code in a yahoo store?
By drifterz28
, October 7, 2011 @ 8:31 pm
This code belongs as a PHP page on the webhosting side
By adam75dfw
, January 26, 2012 @ 6:24 pm
I 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.
By E-comm
, February 4, 2012 @ 12:29 pm
so 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