Table based USPS shipping calculator: Finished.

Posts: 95
Joined: 08/07/2007
Uber DonorBug FinderEarly adopter... addicted to alphas.Spreading the word - Ubercart for president.

This is G o o g l e's cache of http://www.ubercart.org/forum/development/1334/table_based_usps_shipping... as retrieved on Aug 1, 2007 14:20:29 GMT.
G o o g l e's cache is the snapshot that we took of the page as we crawled the web.
The page may have changed since that time. Click here for the current page without highlighting.
This cached page may reference images which are no longer available. Click here for the cached text only.
To link to or bookmark this page, use the following url: http://www.google.com/search?q=cache:zuwSYbTG9b8J:www.ubercart.org/forum...

Google is neither affiliated with the authors of this page nor responsible for its content.
These search terms have been highlighted: 07 24 2007
Ubercart

* Home
* Documentation
* Forums
* Issues
* Livetest

Home » Forums » Ubercart » Development
Table based USPS shipping calculator: Finished.
Submitted by TheOverclocked on Mon, 07/09/2007 - 23:33
TheOverclocked

Posts: 73
Joined: 05/26/2007
Getting busy with the Ubercode.

Well I have finally finished another module for Ubercart tonight. This module has internal rate tables to calculate shipping based on the zip code and weight of the package. As of now, only USPS Priority Mail is integrated. The rates are based on zones, so the module will calculate the zone based on the zip code you give it.

This module is needed for calculating shipping for Google Checkout. If you used an online rate calculator, it would take too long to get a response and Google Checkout would not show any price for shipping. Google Checkout gives a 3-second window for you to calculate shipping and send the prices back, so a table based method is necessary.

Included is a rate file with the latest prices. The module will show you which definitions you have installed, and also it will show what the latest definitions are so you will know when the rates have changed. To install new rates you simply purge the rates table and install the new file. The rate files are fingerprinted and checked against the fingerprint specified via XML to ensure the file has not been tampered with. Very high tech Sticking out tongue

To import the rate file follow these instructions:

1. Drop the rate file in your /modules/ubercart/uc_usps_table folder (the provided file is already there)
2. Browse to /admin/store/settings/quotes/usps_table/import and find the file in the drop down and click "Import Rates"

The single largest caveat at this point in time is getting the correct zones based on the origin zip code. The zone charts with USPS are dynamic and just depend on the origin zip code so everyone will have different zone definitions. To install the zone definitions for your origin follow these instructions:

1. Head over to the USPS Zone Charts and insert your origin zip code: http://postcalc.usps.gov/Zonecharts/
2. Click "Get Zone Chart" and then "Print Zone Chart" (this will make a printable html page)
3. Save the page and rename the file to "zip.html"
4. Drop your zip.html file into your /modules/ubercart/uc_usps_table folder
5. Browse to /admin/store/settings/quotes/usps_table/zimport and click "Import Zips"

As of now, this module does not integrate with the checkout at all. It will only work with the upcoming Google Checkout module. If someone would be kind enough to add the Ubercart Shipping Hooks in, it would work just fine. Everything is there, it just needs the API connection.

The two most important functions in the module:

uc_usps_table_calc_zone($zip)
uc_usps_table_calc_rate($weight, $zone)

The first one will get the zone based on the 3 or 5 digit zip code, and the second will get the rate based on the weight and zone. Everything else is pretty self-explanatory, I have heavily commented the module to help developers.

Hope this module makes it Smiling Let me know of any issues found.
Attachment Size
uc_usps_table.tar.gz 8.81 KB
--

Drupal and vBulletin have united. Meet Drupaltin.

* Add new comment

permalinkTue, 07/10/2007 - 07:32
Ryan

Posts: 1665
Joined: 09/26/2006
AdministratorCode Monkey Head - I eat bugs.

"user warning: Key column 'zone' doesn't exist in table query: uc_usps_table_install CREATE TABLE uc_usps_table_rates ( `weight` TINYINT(2) NOT NULL, `rate` VARCHAR(500) NOT NULL, PRIMARY KEY (`zone`) ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ; in /www/vhosts/livetest.ubercart.org/includes/database.mysql.inc on line 172."

Minor issue... setting a primary key on a table to be a non-existent column. But it keeps it from working. Sad Zone importing worked just fine, though.

* reply

permalinkTue, 07/10/2007 - 07:43
TheOverclocked

Posts: 73
Joined: 05/26/2007
Getting busy with the Ubercode.

Whoa sorry about that. I had the wrong install file in there. I updated the tar.gz with the new install file and attached it to the first post Sticking out tongue
--

Drupal and vBulletin have united. Meet Drupaltin.

* reply

permalinkTue, 07/10/2007 - 18:46
druru
Posts: 220
Joined: 03/21/2007
Brain Stormer

toc,

i'm new to the whole ecomm shipping thing, so bear with me on the questions here. i've read through your post and went to the usps links for the zones and think i understand how this works but wanted to clarify a few things:

I understand the concept of a distance zone -> rate * weight = shipping. I understand how one needs to go to usps site and download the zone (and probably rate) files to make this work.

1) where / how does one get the current rate table for a given zone at usps?

2) above you state that teh code will "know" when the rates have changed. how does it know and does it download a new file from usps daily to keep it in synch?

3) same question for the zone file. i don't think the zones change often but i imagine occasionally they might or that a new zip code is added as a new town is incorporated or removed as an existing town vanishes in the real world. since i have to manually copy paste the zone file, i imagine that this is not automated?

4) same question for the master list of US zip codes. where does one get this and how often should it be checked for updates?

5) You say your module only incorporates priority shipping. If the majority of my business is going to be normal mail / ground delivery shipping, what do i need to do to modify your module to work with that? I imagine i have to get the rate table for regular mail shipping (not sure of the lingo the usps uses for that service btw) - as per question above, where / how do i get those rate tables for the different services?

Then i imagine i just have to enable the new service via a module hook. but when i look at these as described above:

uc_usps_table_calc_zone($zip)
uc_usps_table_calc_rate($weight, $zone)

there is no argument for the type of service - so how do i differentiate the calculation for different types of service? especially if i'm going to offer BOTH priority and regular shipping, not just one or the other.

6) Finally, another usps module is floating around here as well. I believe that version directly interacts with an online usps webservice at checkout time though. I understand why you chose NOT to go with an online version due to google checkout time constraints.

I'm assuming everything you're doing with your rate/zone files in your module can automatically be done via a web service directly with usps online (which means you always have access to the latest greatest rate/zone/zip data) at checkout time right?

If so, any reason why i would want to use your module in place of the online web service method / usps module if i have a choice?

i ask this as stated before not having any real world experience with online shipping via usps and am getting ready to do so soon.

also because it seems like the posts i see here regarding the online usps shipping seem to indicate problems (probably via less mature code support at this point from uc due to focusing on other things) and would like to pick the method that seems the best.

using usps web services, there will always be some timing issues at checkout based on waiting for usps to return the shipping net request before the order can be completed. the benefit is you always are in synch with usps's current rates/zones/zips, etc.

on the other hand, your table based module is probably nice and simple, fast and doesn't depend on a functioning link (all the time)to usps. which is very appealing. the only drawback is being in synch with latest greatest usps data.

if anyone else wants to chime in on how the usps code that is built into (i think) alpha 7 is working that's great too.

thanks

* reply

permalinkWed, 07/11/2007 - 02:21
TheOverclocked

Posts: 73
Joined: 05/26/2007
Getting busy with the Ubercode.

druru, Ill attempt to answer your questions fully but if I miss something let me know.

1. The current Priority rate file is included in the download. If the rates are changed, I will release a new rate file for download to bring your prices up to date. All pricing is listed on this page. As you can see from that page the prices are just a mess and I had to write a special code to extract the rates from the html source Eye-wink
2. It will be in the module configuration. Here is a screenshot of what I mean. The rates do not change every day so there is no need to sync every day.
3. Zones don't change often either, but when they do you can just follow those instructions from the first post and import the latest zone definitions for your zip.
4. Not sure what you mean by a "master list" but the page you visit to get your zones will give you a latest effective date.
5. I developed this module for my client who will be using only Priority mail, hence I did not take into consideration other shipping methods. Other methods could definitely be added, but you would have to rearrange the database a bit. Perhaps in the rates table, you could add another column for the service type and when selecting the rate you could use the argument in your mysql query to find the correct service/rate. Not a ton of modding would be needed.
6. You pretty much nailed the reasons in your words here. The online is going to be guaranteed, but honestly even when the prices do change they don't change all that drastically. Any time you rely on the online calculator, it is a weak link in the chain. Myself? I'd rather not rely on that weak link if I don't have to. If a customer can't get their shipping costs online it only causes them havoc which not a single customer in the world will stand for.

--

Drupal and vBulletin have united. Meet Drupaltin.

* reply

permalinkWed, 07/11/2007 - 06:01
druru
Posts: 220
Joined: 03/21/2007
Brain Stormer

toc,

i think you covered everything. thanks.

out of curiosity, what kind of client only wants to offer their customers priority shipping? or maybe better put, what kind of product sold only dictates priority shipping? as a customer, i think i'd like some options.

* reply

permalinkWed, 07/11/2007 - 12:42
TheOverclocked

Posts: 73
Joined: 05/26/2007
Getting busy with the Ubercode.

Coffee/tea

No need for rush delivery Sticking out tongue
--

Drupal and vBulletin have united. Meet Drupaltin.

* reply

permalinkWed, 07/11/2007 - 18:07
druru
Posts: 220
Joined: 03/21/2007
Brain Stormer

toc,

i obviously don't know my usps lingo / terminology. Eye-wink

i assumed that priority == rush delivery (maybe what usps considers express). but by your last reply, i guess priority == basic or standard shipping. meaning this module is setup out of the box to provide standard shipping (the cheapest/slowest possible) on basic items like tea/coffee. yes?

[edit my original post]

i just returned from a trip to the link you specified above:

http://pe.usps.gov/text/dmm300/ratesandfees.htm

uhm.. i guess parcel post is the cheapest and priority is faster more expensive. yet there is something called first-class mail which is below priorty mail yet priority mail is not available on DISCOUNT parcels?? wtf? how come parcels aren't retail - i'm assuming a parcel is a parcel and that normal retail customers can ship parcells?

man what a confusing mess that is (for the uninitiated). is it supposed to be easy to understand the myriad of options here or am i just dense? what's the difference between machinable and non-machineable? discount and retail (prices are same for parcel post???)? how come there's a standard rate for discount but not retail? wtf? Smiling

guess i'm going to have to spend some time at my local p.o. figuring this mess out unless someone can enlighten me in a couple of sentences or less Eye-wink

oh and another thing.. that usps website is the most god awful slow thing.. probably why other folks here might be having problems with their online api service. looks like i might be using your module Smiling

* reply

permalinkWed, 07/11/2007 - 22:51
torgosPizza
Posts: 32
Joined: 07/05/2007

oh and another thing.. that usps website is the most god awful slow thing.. probably why other folks here might be having problems with their online api service.

I can safely say, that's not the issue. We currently use USPS for our website - www.rifftrax.com - and the quotes are instantaneous.

This module looks interesting, but I'm wary about not offering live, real-time rate quotes...

* reply

permalinkWed, 07/18/2007 - 22:58
TheOverclocked

Posts: 73
Joined: 05/26/2007
Getting busy with the Ubercode.

Tonight I have released version 1.1 of this module. Please check it out, I think there are some new things you will like!

druru,
Yes USPS Priority Mail is basically the same as UPS Ground. They do have a cheaper/slower method I believe, but it is extremely slow and I believe there are more restrictions. They have a bunch of services but the most popular and widely used is Priority mail. I can't recall a time I've ever used the others. Priority is only a buck or two more usually so I just use that.

That site is pretty slow for sure. Especially the rates page because it has so much data on it.

torgosPizza,
Now worries anymore with 1.1 Smiling
--

Drupal and vBulletin have united. Meet Drupaltin.

* reply

permalinkThu, 07/19/2007 - 02:20
druru
Posts: 220
Joined: 03/21/2007
Brain Stormer

OC,

thanks for the work on the module.. also, i really liked the way you created the contributions page. it's very clear and self - explanatory for those that want to use your module.

couple of points (i thought i'd put them here instead of crowd out the new contrib page):

1) i like your cron update! nice.. just thought i'd point out that it's entirely possible that the table structure (html) might change in the future which could screw up the whole auto ability. but that will be obvious when it happens and not likely to happen often.

2) I think your statement "All I ask in return is that someone please code in the Ubercart shipping hooks so that this can be used with regular checkout." means that as it stands now, this module is meant to work ONLY with google checkout. so for instance, if my site is going to use paypal web standards pro, then i need to modify your module to calculate the shipping on the checkout page at some point on the page.. either in the timespan between when you click submit to fire off the order and before it's sent to paypal or actually on a presubmit verify page - right?

* reply

permalinkThu, 07/19/2007 - 03:01
TheOverclocked

Posts: 73
Joined: 05/26/2007
Getting busy with the Ubercode.

druru,

I took into consideration that the html structure could change and mess things up. The good thing is that they changed on July 15 and my code still works flawlessly. However, in the case of the page changing enough to throw the code off the module will skip auto update. You ask how? Well I coded a nice little feature in the module that checks my website for a green light before it runs the update. In fact the page it checks is here: http://www.theoverclocked.com/uc_usps_table.php and if the contents is a "0" which is is now then it will go through with the update. If the isn't then it will skip the update and in the shipping panel it will report "Module out of date!" So how do you like them beans Cool

Honestly I have not looked at the paypal module so I could not say how it calculates the shipping. If it runs off of the Ubercart shipping hook, then once this module gets that hook it will work. If not then you would have to slightly modify the module to implement this shipping module. My opinion of the situation is that the Ubercart shipping hooks need some changes to be more flexible. I actually made a hook for the Google Checkout module "hook_googlecheckout_ship" for hooking in shipping modules. After I made it I started to think that it is sorta redundant with the Ubercart shipping hooks, but as is I couldn't use the Ubercart shipping hook for what I wanted either. So I just left both out for now.
--

Drupal and vBulletin have united. Meet Drupaltin.

* reply

permalinkThu, 07/19/2007 - 03:36
druru
Posts: 220
Joined: 03/21/2007
Brain Stormer

got it.. cool..

suggestion.. you're going to have every one that uses your module sending requests to your site. it might be better to take your php file and include it into your module download so that others can run that file locally and have the module check against their own local copy instead of going to your site.

i'm probably going to be getting to the usps stuff soon on my own site.. so i'll take a look at incorporating the hooks for your module unless someone beats me to it..

thanks

* reply

permalinkThu, 07/19/2007 - 03:47
TheOverclocked

Posts: 73
Joined: 05/26/2007
Getting busy with the Ubercode.

I think you might have misunderstood what I meant. Basically the module doesn't rely on the request to my site at all. If it doesn't connect for some reason, so be it. But if the rate page changes and the module cannot correctly extract the data anymore, then I can edit that file on my site to globally disable auto update for everyone using the module. That way instead of corrupting their rate tables I can disable the auto update function for you and tell you the module needs update Sticking out tongue

It would be pointless to include the file with the module because if you want to disable auto update there is an option in the settings panel for that Smiling

I'd greatly appreciate it if you integrated the shipping hooks. Thanks.
--

Drupal and vBulletin have united. Meet Drupaltin.

* reply

permalinkTue, 07/24/2007 - 09:36
mizru
Posts: 10
Joined: 07/18/2007
Uber Donor

Hi! I'm trying out your module since I can't get the USPS module to work... I have installed the module, put in the rates info and zip code info, and now I don't see the USPS shipping method in admin/store/settings/quotes/methods. Any ideas? Thanks!

* reply

permalinkTue, 07/24/2007 - 19:08
TheOverclocked

Posts: 73
Joined: 05/26/2007
Getting busy with the Ubercode.

mizru,

The panel is located at /admin/store/settings/quotes/usps_table. Remember the Ubercart shipping hooks have not yet been integrated.
--

Drupal and vBulletin have united. Meet Drupaltin.

--