UPS Rates and Services API and Spry

Two days in a row. This is the best I have done in a while. I’m not sure what you will be most excited about today, whether it’s going to be for showing you how to use the UPS Rates and Services API, or whether it’s using Spry to do it in real-time, or whether it’s the fact that my stuff makes you smile. Anyways, here is a link to a working sample of my Spry’d UPS Rates and Service API form.

The Problem:
Too often checkout pages are too many steps and too long. Without me getting into it personally, I will give you a tip that will make your online stores more successful: Limit the checkout process to one page. Recently I had a client I built a custom shopping cart for. She didn’t need a robust system like Os Commerce or Zen Cart, so I built one in from scratch. When I came to building the checkout page, I kept asking myself “how can I make this easier for the front-end user”. After fitting everything into one page, and realizing I could use Spry to get shipping rates RIGHT after they put in their zip, I knew I was on my way to land a home run.

The Solution:
I guess there are two parts to this one. The first is how the freak do I use the UPS API of which they supply close to 70 pages of documentation? Well, first off read this article and download his code. I am not going to take credit in creating the function I use. He also takes you through what you need to do on the UPS website, how to configure his script, and how to use his function.

Mark’s script however, simply returns one rate. I wanted to create a way to display several rate options to my customer. You can see what I did on the example page, below the form. So basically, I create an array with all of the shipping options I want to display. Then I run a foreach statement, to read the array one by one and complete Mark’s function. Each one returns a result that I can then display back to the user. However, I added in a radio button for each one, as well as the name of the shipping method next to it with the returned price from the API.

The next step was to integrate it using Spry’s updateContent utility. Like I showed yesterday this is fairly simple. I use the “onblur” handler again for their zip code, and place the zip code as the second input box to give it some time to load. Here is the “checkShipping” function that is called:

var checkShipping = function () {
//get their zip from the ship_zip box
var zip = document.shipping.ship_zip.value;
document.getElementById("Rates").innerHTML="Loading shipping options for Zip: " + zip + " ...";
//only check the zip if it's more than 4 characters
if(zip.length > 4) {
window.Spry.Utils.updateContent('Rates', 'getUps.php?zip=' + zip);
}
}

It’s pretty simple. Basically I get the value of their zip, and then display some temporary data into my loading div, to alert the user that something is coming. Then I check that their zip is more than 4 characters, and then I use my getUps.php script to retrieve the info. I pass that same zip variable that is being used to show the customer I am retrieving info for that zip, into the php script and then into the function to be used in calculating shipments.

As always, I am sure you are already thinking of ways to extend this further. Better validation maybe? Using sessions to remember their selection and display it if they have to come back and edit the page again? Maybe putting an awesome animated gif of a flying eagle above the form? Hopefully this will help you on your journey, and you won’t be forced to use OS Commerce or Zen Cart just because they have a plugin that does this same thing.

Stay tuned tomorrow as I discuss how Spry can be used to display very nice alert messages. I might even wrap it up into a PHP class.

This entry was posted in Code, Featured and tagged , , . Bookmark the permalink.

5 Responses to UPS Rates and Services API and Spry

  1. Adarsh says:

    Hi Pat,
    I know little programming. But I’m always keen to learn such tips and tricks. Very nice use of Spry. Good job :)
    I’ll keep returning for further reading.
    Adarsh.

  2. Mettled says:

    I’m getting “Call to undefined function: ups()” as a response when I try to do this on my own site. Can you help me out? I’m assuming I need to pass my UPS account info somehow.

  3. Mettled says:

    Ok, I’m obviously not a coder. I copied in what I had from Mark Sanborn’s code and everything is working fine. I want to thank you a million times for posting this article. I have spent weeks trying to get shipping rates to function on my site. It’s shocking to me that it isn’t easier or at least better documented since it’s such a common aspect of online shopping. One final comment: I want to integrate UPS international charges (I guess activated by the country?) as well as USPS domestic and international charges but I have no idea how to do this. If you feel like a hero you should give that a whirl. I did find some resources that can accomplish all of this but they are based on subscription payments, which is not affordable for small time people like me. Thanks again!

  4. patrick says:

    @Mettled: Thanks for your comment. I am guessing the error is being caused because you need “require_once(“ups.php”);” on the top of your getups.php script. I just realized I left it out in my example, which may be giving other people problems.

    I would love to jump into international stuff for you, however I am too busy with my current project, http://www.BlvdStatus.com.

  5. Adam says:

    Patrick,
    I have been looking for a good answer to load shipping and came across this page. I have been messing with it and I seem to have a problem getting it working. It continues to say loading shipping options. However, it never returns any values. I already have the ups API working through php. Any help at all would be great.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>