Shipping Modules are fun to code

By | December 18, 2008

Here is a question from a client;

Im setting a a new oscommerce website and cant seem to get the shipping working. I would need to have about 5 zones. Each zone would contain different countries. For each zone there is a basic rate per pound. eg $35 . Each additional pound cost $5 etc per zone. That cost differs per zone. Also i would need packaging cost.

I’ve not seen anything quite like this available as a contribution – so instead of spending hours looking for it, I decided it would be more cost effective to code this up from scratch. It was actually fairly straightforward. Here’s the interesting info;

In the Admin section, I simply made an input form for the module. For each Zone that is set up, the client can insert 3 different prices. In the image below, I have set up 2 zones…

As you can see, each Zone has 3 inputs: “1st”, “rest” and “handling”. “1st” takes care of the first unit of weight, and “rest” takes care of any subsequent units. Handling is obvious! In Zone 1 (in the image) I set up 3 countries: AG,BB,DM. So the costings inserted in the three price fields will be used for those 3 countries. In Zone 2, similarly, 3 countries US,CA,FR with different prices for “1st” etc.

You will also notice above these Zones, an area entitled “Cost for all other Zones” – this is a fail-safe method of making sure that each country gets a quotation for postage. A country that is not in any existing zone will default to this.

In the actual codebase for the module, the relevant piece of code that works out the actual price is as follows:

[php]$shipping_cost = $shipping;
if ($shipping_weight > 1) $shipping_cost += ($shippingtoo * ($shipping_weight – 1));
$shipping_cost += $handling;[/php]

Which basically says;

[php]$shipping_cost = $shipping;[/php]

This is for items of a total of 1 unit of weight or less

[php]if ($shipping_weight > 1) $shipping_cost += ($shippingtoo * ($shipping_weight – 1));[/php]

Here we are saying IF the total weight is GREATER than 1, then $shipping_cost should be INCREASED by the “rest” ($shippingtoo) amount multipled by the $shipping_weight -1. Remember that we have to do -1 as the first unit has already been worked out!

[php]$shipping_cost += $handling;[/php]

Easy and straightforward, add the handling fee on top!

Code hints

You might notice me using something weird in these calculations, the += sign. What this does is simply add something onto an existing total. It’s a shorthand way of saying;

[php]$shipping_cost = $shipping_cost + $handling;[/php]

It’s a bit less work to just say

[php]$shipping_cost += $handling;[/php]

So, there you have it. I thought it might be interesting for you to see a bit about how I code things up – I try to make things as simple as possible.

Why not just use “Zone Rates” module?

If you have a bit of knowledge about osCommerce, you are probably thinking “he could have just used Zones Rate”. Well, that’s true, but there are good reasons why you should not…

1. Using Zones Rate you would have to set up EVERY country into a Zone to get a shipping quote. In mine you do not need to do this. Each country NOT in a Zone will default to “all other zones”.

2. Zones Rate cannot handle an open-ended system of weight, as the database field is simply too small – so if the customer ordered 400 units, with each unit costing say $5, my version can work this out very easily. Using the standard Zones Module would be IMPOSSIBLE (under normal use).

3 thoughts on “Shipping Modules are fun to code

  1. Michael Clayton

    The courier were using has advised us to provide shipping to our customers on zones within the UK and not weight. I’ve just stumbled upon ‘ClubOsc’ and I’m wondering if I could adopt a similar approach, incidentally how do you make an input form for the module?

    The UK is divided into 5 major zones what in your view is the best approach?

    Thanks in advance


  2. Gary Post author

    MC – could be problematic.

    You’d need to compare the courier zones against the list of counties in the UK. Som counties might have half in one zone and half in another for example.

    Set up 5 UK Zones. In each Zone place each county appropriately. Now use any of the shipping modules to get the price you need to charge, eg: flat.php (if the charge is a flat rate for example). Duplicate flat.php 5 times (not overly difficult) and assign one to each of your 5 zones.

    Clear as mud?

    If you need help setting it up, email me, my address is up there ^

  3. Michael Clayton

    Hi Gary
    Thanks for your prompt response. I’m going to need your help. I’ve found a osc contribution that lists all the UK counties into zones, I have made the comparison with the courier zones and indeed some counties straddle two zones, but I can handle that.
    The zone contribution is in the form of sql insert statements, a sample of which follows:

    INSERT INTO zones (zone_country_id,zone_code,zone_name) VALUES(“222″,”Avon”,”England – Avon”);
    INSERT INTO zones (zone_country_id,zone_code,zone_name) VALUES(“222″,”Antrim”,”N. Ireland – Antrim”);
    INSERT INTO zones (zone_country_id,zone_code,zone_name) VALUES(“222″,”Aberdeen City”,”Scotland – Aberdeen City”); etc

    The question is how do I set up the 5 UK Zones, and in each Zone “place each county appropriately”?
    The price I need to charge is indeed a flat rate, you’ve explained that I’ll need to duplicate flat.php 5 times and assign it to each of my 5 Zones. How do I do this and why?

    Thanks in advance


Leave a Reply

Your email address will not be published.