Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am programming another website for the company I work for. This one is going to use both UPS and the United States Postal Service for shipments. We have only one product right now, but I need to program it to be scalable so when we do add additional products we can just plug in the products.

I would like your advice on the best way to programically structure the ordering system.

Here are my thoughts.
Since the product is a 4oz glass bottle with a high quality glass dropper, we have to package these in boxes. We are going to only allow product quanities of: 1,2,3,6 and 12. We figured out that 1-3 quanities will all fit in one box (4" x 4" x 6") and if they get 6 or 12 it will fit in another box (12" x 6" x 8"). My problem with this is that if I hard code it to do these, then if we add additional products I would have to take the website offline to make it work. Therefore here is how I see it in my mind:

Make the database that stores each product have a 'dot notation' type field, like libraries use:

qty|length|width|height|weight
Example:1|4|4|6|11.9 # Quantity of 1, box: 4x4x6 and weighs 11.9oz.
so I could make a text field that can store all the different values for that product. or I guess I can make another table that will have the product code so it is tied to that product, then have it structured this way:
CREATE TABLE product_quantities ( pqid INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, pcode BIGINT UNSIGNED, qty INT UNSIGNED, length DECIMAL(3,2), width DECIMAL(3,2), height DECIMAL(3,2), weight DECIMAL(3,2) );
Then foreach different quantity that a product code is allowed to have it will show the size of the box and the total weight of that quantity, that way I can pass it to UPS and USPS to get the shipment prices to give them a choice.

The big problem I have with this, is if we do add additional products and they purchase say 6 bottles of the real water, then 2 of whatever else we have, how would I be able to get my programming to figure out what size box that would combine to require? Do you know of a way to do it?

Also, I found a bunch of shipping modules for UPS and USPS resepectivly. It is crazy however, that I tested how much the shipment would be with Net::UPS for this: 4x4x6 box, weighing 1LB 12OZ. and from the shipping center in Oregon, to my house in Oklahoma. It came up with 17.99 and when I logged on to UPS to our account and did the same shipment it came up with 13.99 I could not figure our why such a difference.

So do you know how companies like TigerDirect, and many others get accurate prices to show in a drop down for the customer to select from?

I would appreciate any feed back on this too.

Thank you,
Richard

Replies are listed 'Best First'.
Re: Seeking advice about Shipping and Product Purchases
by hangon (Deacon) on Jul 20, 2008 at 09:59 UTC

    This isn't really a Perl answer, but you should learn a bit about the shipping business before you start programming. First, forget about box sizes. Unless you're shipping oversize packages you only need to be concerned about weight. Visit your shipper's website to see what they determine to be oversize. Also, except sending very small packages by USPS, shippers round fractional weight up to the next pound. With that simplification you have better options, but these involve estimation and policy, which may be out of your influence:

    • Just add a little more weight in your database for each item to compensate for the packaging.
    • Write an algorithm or create a table to estimate packaging weight based on product quantity & mix.
    • Estimate shipping costs and use that to mark up the product price, then advertize free shipping

    With mixed products & quantities you cannot compute exact shipping parameters beforehand unless you can account for every possible combination. Most companies either do some kind of estimation, or weigh each completed package before computing shippping costs. Over time, you can use empirical data from actual shipments to fine tune your estimates.

    Shippers have many pricing scales and many extra cost options you can add on. Businesses with shipper accounts receive significant pricing discounts based on their shipping volume. UPS is generally the most expensive shipper, but even $13.99 sounds too high unless you asked for some added options.

Re: Seeking advice about Shipping and Product Purchases
by CountZero (Bishop) on Jul 20, 2008 at 08:58 UTC
    From a database structure point of view, it is best to have another table with the dimensions, weights, ...

    However that doesn't solve your problem if you want to combine different products into one package.

    What you are asking is solving a three dimensional puzzle. If you want to do it exactly it will be quite difficult, although there is probably some mathematical algorithm that will just do so.

    A few quick checks however will allow you to approximate the box:

    • The box must have a volume larger that the total volume of the products (including packing)
    • the largest length of each product must be less than the largest "length" of the box.
    Such a quick check will probably already eliminate a number of boxes. Then add a suitable "safety margin" and take the smallest box that still remains. That will probably be OK for 90% or so of all cases.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James