Half Kilos in osCommerce shipping

Yesterday I was asked by a client to create quite a complicated shipping module. The module (now it’s completed) is a mash up of zones (but by states not countries), by weight per kilo and half kilo, via two separate couriers. It basically covers 3 different zones, in zones 1 and 2 are a number of states – these are charged by the kilo at $x and the first 3 kilos are charged at $y.


Zone 1:
First 3 kilos (or part thereof): $10
Extra Kilos above 3: $2.50 per kilo

Similar for Zone 2. Zone 3 is different in that the first 3 kilos are charged at $20, and each HALF kilo (or part thereof) thereafter is charged at $3. This brought a difficulty to the codebase – as if the total weight was say 5.8kgs, the charge should be:

First 3 kilos: $20
Next 3 kilos: $3 per half kilo
Total: $38

What was actually being charged was:

First 3 kilos: $20
Next 2.8 kilos: $3 per half kilo
Total: $36.80 [being $20 + ((2.8 * 3)*2)]

So what I had to do was find a way to round up any weight to the next nearest 1/2 kilo (unless of course the total weight is already a full kilo or a half kilo. So, for example:

3.0 stays at 3 kilos
3.1 is increased to 3.5 kilos
3.2, 3.3, 3.4 all increased to 3.5 kilos
3.5 stays at 3.5 kilos
and so on

This proved more difficult than I thought, but after a bit of lateral thinking I came up with:

[php]$num = 3.2;
$down = $num/0.5;
echo ceil($down)*0.5;[/php]

In the code, we are doing the following…

Assuming that we are looking to round 3.2, we divide 3.2 by 0.5. This gives us 6.40. Then we use the ceil function of PHP to automatically increase this to the next nearest integer which is 7. Then we multiply 7 by 0.5 to get the end result which is 3.5.

Sounds more complicated than it is! What this enabled me to do was set up zones differently for charging the correct amounts. Here is some of the codebase of the new shipping module that I created;

[php]// dest zone = 1 or 2
$shipping_cost = (($shipping) * (ceil($shipping_weight)-3)) + $handling;

// dest zone = not 1 and not 2
if ($dest_zone == 0) {
$the_weight = $shipping_weight-3;
$down = $the_weight/0.5;
$up = ceil($down)*0.5;
$shipping_cost = (($shipping) * (($up)*2)) + $handling;

$shipping_method = MODULE_SHIPPING_ZONES_ZCBK_TWO_TEXT_WAY . ‘ ‘ . tep_get_zone_name($order->delivery[‘country’][‘id’], $order->delivery[‘zone_id’], ”) . ‘: ‘ . $shipping_weight . ‘ ‘ . MODULE_SHIPPING_ZONES_ZCBK_TWO_TEXT_UNITS . ‘ (‘ . $way . ‘)’;[/php]

The above won’t mean much to non-coders, but it’s possible to see that I am doing a number of things within this module that are completely non-standard osCommerce. Shipping Modules are quite straightforward to code from scratch – I suppose that it’s one of my specialities!

So, if you need some complicated method of shipping coded up, feel free to contact me.

One Reply to “Half Kilos in osCommerce shipping”

Leave a Reply

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