Jump to content
  • Checkout
  • Login
  • Get in touch


The e-commerce.



Recommended Posts

Date: 08-07-03 12:00nn


To: Everybody


Subject: EMS shipping calculation


From: sigma (e-mail: [email protected])



Language: PHP








This function solves the traditional table-driven Shipping Module in an e-store. Usually in an OS-comerce shipping, values in a particular shipping method are stored in a table/array editable to the admin only.

This table is limited to 255 characters only.


Values format is: 2:14.05, 3:15.03, etc... up to 255 characters. Meaning: if the order is 2kg or below, the shipping fee is $14.05; from 2kg to 3kg the shipping fee is $15.03... and so on... there goes your table so long...


What if the customer order many items, which is not in the table list anymore? You just can't extend the table! Nor change the configuration setting to accomodate more than 255 characters.


That is why, I've written this small piece of function code to continiously calculate the shipping cost, no tables involved... just pure computation, so you need no more table hassles as described above.


However, I don't know to integrate this small function into the entire os-commerce system. What directory to put to, what configuration to enable/ disable, etc.. PLEASE HELP ME!


How this function works:


Function CALCEMS.PHP computes the shipping cost of a delivery

FROM (origin): Philippines

TO (destination): any part of the world,

using the EMS delivery system.


The function takes two input parameters: country code & kilogram

The function outputs: shipping cost in US Dollars $


See below for the exact coding of CALCEMS.PHP


To call the function, another outside program is needed passing the necessary parameters. For example,


<?php require('calcems.php');


print "This is the answer: $$joey";



The above small piece of code assumes CALCEMS.PHP was already defined and/or running in the background.


Create the above small code in Notepad name it, say, MYCODE.PHP, and fire it on your browser. Also calcems.php is assumed to be existing in the same directory.


The first line : require('calcems.php');

defines the function CALCEMS.PHP


The second line: $joey=calcems("US",25);

calls the function calcems.php passing country code: US (United States) and 25 kilograms is the weight.


Output: after calculation, here is the result

This is the answer: $265.23


Method of Computation:


The method of computation used was from EMS (Express Mail Service), a very active delivery/ courier/ forwarding company in the Philippines attached and recognized by the PhilPost (Philippine Postal), the government agency responsible for managing mails and packages in-out of the Philippines.


A table provided by EMS was used and embedded inside the function. All figures entered are in Philippine peso.


Inside the function you see several lines, containing the country definition, like this line:


array ("name"=>"US","val2k"=> 1800,"val2kadd"=> 315,"val10kadd"=> 245,"maxweigh"=>30 ),



country name: "US"

value 2k : 1800 (pesos)

additional value if higher than 2k: 315 (pesos) per 500g increase

additional value if higher than 10k: 245 (pesos) per 500g increase

maximum weight allowed for this country: 30 kg.


Each country has entry in the array. Hence there is a long list of this country values at the beginning of the function program.



1. Outside program calls CALCEMS.PHP passing parameter CountryCode & weight

2. CALCEMS.PHP checks if the country code is in the array list

3. If it is the list, continue, otherwise exit the function with zero value.

4. For-loop up to max.country

Search the country array list

calculate shipping cost

a. from 2ki-10k

b. 10k-max.weight

c. excess of the max.weight

c.1. reset all settings to zero

c.2. do calcualtion again as in 4a & 4b

accumulate all result of the computation

End-for loop

5. convert peso output into US dollars

6. returm to the calling program bearing the US$ result


Scope: Philippines settings





All variables, arrays, tables inside the program bears figures / amount in Php (Philippines Peso).


The output is in US Dollars $.


There is a variable called $pesotodollar=53.5; defined. This is the rate of Peso-Dollar exchange rate today. Before flashing the output,

the result (Philippine peso) is divided in this variable to give an US$ output.


Program function code:




function calcems ($councode,$weightx)


$countries=array (

array ("name"=>"MO","val2k"=> 760,"val2kadd"=> 140,"val10kadd"=> 70,"maxweigh"=>30 ),

array ("name"=>"HK","val2k"=> 740,"val2kadd"=> 130,"val10kadd"=> 60,"maxweigh"=>30 ),

array ("name"=>"BN","val2k"=> 740,"val2kadd"=> 130,"val10kadd"=> 60,"maxweigh"=>10 ),

array ("name"=>"ID","val2k"=> 940,"val2kadd"=> 170,"val10kadd"=> 100,"maxweigh"=>30 ),

array ("name"=>"TH","val2k"=> 910,"val2kadd"=> 140,"val10kadd"=> 70,"maxweigh"=>30 ),

array ("name"=>"TW","val2k"=> 910,"val2kadd"=> 140,"val10kadd"=> 70,"maxweigh"=>30 ),

array ("name"=>"SG","val2k"=> 870,"val2kadd"=> 150,"val10kadd"=> 80,"maxweigh"=>30 ),

array ("name"=>"MY","val2k"=> 870,"val2kadd"=> 150,"val10kadd"=> 80,"maxweigh"=>30 ),

array ("name"=>"CN","val2k"=> 1163,"val2kadd"=> 155,"val10kadd"=> 85,"maxweigh"=>30 ),

array ("name"=>"KP","val2k"=> 1152,"val2kadd"=> 155,"val10kadd"=> 85,"maxweigh"=>30 ),

array ("name"=>"KR","val2k"=> 1152,"val2kadd"=> 155,"val10kadd"=> 85,"maxweigh"=>30 ),

array ("name"=>"JP","val2k"=> 1150,"val2kadd"=> 150,"val10kadd"=> 80,"maxweigh"=>30 ),

array ("name"=>"LA","val2k"=> 1094,"val2kadd"=> 150,"val10kadd"=> 80,"maxweigh"=>20 ),

array ("name"=>"VN","val2k"=> 1093,"val2kadd"=> 134,"val10kadd"=> 64,"maxweigh"=>20 ),

array ("name"=>"MV","val2k"=> 1286,"val2kadd"=> 200,"val10kadd"=> 130,"maxweigh"=>20 ),

array ("name"=>"LK","val2k"=> 1262,"val2kadd"=> 180,"val10kadd"=> 110,"maxweigh"=>30 ),

array ("name"=>"IN","val2k"=> 1254,"val2kadd"=> 180,"val10kadd"=> 110,"maxweigh"=>35 ),

array ("name"=>"PG","val2k"=> 1240,"val2kadd"=> 210,"val10kadd"=> 140,"maxweigh"=>20 ),

array ("name"=>"AE","val2k"=> 1374,"val2kadd"=> 210,"val10kadd"=> 140,"maxweigh"=>50 ),

array ("name"=>"BD","val2k"=> 1305,"val2kadd"=> 190,"val10kadd"=> 120,"maxweigh"=>20 ),

array ("name"=>"NR","val2k"=> 1301,"val2kadd"=> 190,"val10kadd"=> 120,"maxweigh"=>20 ),

array ("name"=>"BH","val2k"=> 1497,"val2kadd"=> 220,"val10kadd"=> 150,"maxweigh"=>20 ),

array ("name"=>"CH","val2k"=> 1496,"val2kadd"=> 265,"val10kadd"=> 195,"maxweigh"=>30 ),

array ("name"=>"QA","val2k"=> 1496,"val2kadd"=> 220,"val10kadd"=> 150,"maxweigh"=>20 ),

array ("name"=>"AU","val2k"=> 1286,"val2kadd"=> 220,"val10kadd"=> 150,"maxweigh"=>20 ),

array ("name"=>"PK","val2k"=> 1455,"val2kadd"=> 200,"val10kadd"=> 130,"maxweigh"=>20 ),

array ("name"=>"YE","val2k"=> 1454,"val2kadd"=> 230,"val10kadd"=> 160,"maxweigh"=>30 ),

array ("name"=>"CA","val2k"=> 1420,"val2kadd"=> 260,"val10kadd"=> 190,"maxweigh"=>30 ),

array ("name"=>"KW","val2k"=> 1508,"val2kadd"=> 220,"val10kadd"=> 150,"maxweigh"=>20 ),

array ("name"=>"CY","val2k"=> 1512,"val2kadd"=> 250,"val10kadd"=> 180,"maxweigh"=>32 ),

array ("name"=>"SA","val2k"=> 1538,"val2kadd"=> 235,"val10kadd"=> 165,"maxweigh"=>30 ),

array ("name"=>"NZ","val2k"=> 1550,"val2kadd"=> 235,"val10kadd"=> 165,"maxweigh"=>30 ),

array ("name"=>"SE","val2k"=> 1568,"val2kadd"=> 250,"val10kadd"=> 180,"maxweigh"=>30 ),

array ("name"=>"GB","val2k"=> 1570,"val2kadd"=> 270,"val10kadd"=> 200,"maxweigh"=>30 ),

array ("name"=>"IR","val2k"=> 1578,"val2kadd"=> 250,"val10kadd"=> 170,"maxweigh"=>20 ),

array ("name"=>"DE","val2k"=> 1580,"val2kadd"=> 260,"val10kadd"=> 190,"maxweigh"=>30 ),

array ("name"=>"BG","val2k"=> 1600,"val2kadd"=> 280,"val10kadd"=> 210,"maxweigh"=>20 ),

array ("name"=>"EG","val2k"=> 1600,"val2kadd"=> 250,"val10kadd"=> 180,"maxweigh"=>20 ),

array ("name"=>"FR","val2k"=> 1608,"val2kadd"=> 270,"val10kadd"=> 200,"maxweigh"=>30 ),

array ("name"=>"IT","val2k"=> 1620,"val2kadd"=> 270,"val10kadd"=> 200,"maxweigh"=>30 ),

array ("name"=>"DK","val2k"=> 1624,"val2kadd"=> 260,"val10kadd"=> 190,"maxweigh"=>20 ),

array ("name"=>"CO","val2k"=> 1654,"val2kadd"=> 280,"val10kadd"=> 210,"maxweigh"=>20 ),

array ("name"=>"GR","val2k"=> 1680,"val2kadd"=> 260,"val10kadd"=> 190,"maxweigh"=>20 ),

array ("name"=>"AT","val2k"=> 1680,"val2kadd"=> 270,"val10kadd"=> 200,"maxweigh"=>32 ),

array ("name"=>"ES","val2k"=> 1698,"val2kadd"=> 285,"val10kadd"=> 215,"maxweigh"=>20 ),

array ("name"=>"NG","val2k"=> 1724,"val2kadd"=> 300,"val10kadd"=> 230,"maxweigh"=>30 ),

array ("name"=>"MA","val2k"=> 1726,"val2kadd"=> 300,"val10kadd"=> 230,"maxweigh"=>30 ),

array ("name"=>"IL","val2k"=> 1774,"val2kadd"=> 310,"val10kadd"=> 240,"maxweigh"=>20 ),

array ("name"=>"AN","val2k"=> 1793,"val2kadd"=> 270,"val10kadd"=> 200,"maxweigh"=>30 ),

array ("name"=>"NL","val2k"=> 1793,"val2kadd"=> 270,"val10kadd"=> 200,"maxweigh"=>30 ),

array ("name"=>"US","val2k"=> 1800,"val2kadd"=> 315,"val10kadd"=> 245,"maxweigh"=>30 ),

array ("name"=>"NO","val2k"=> 1860,"val2kadd"=> 250,"val10kadd"=> 180,"maxweigh"=>30 ),

array ("name"=>"BR","val2k"=> 2096,"val2kadd"=> 420,"val10kadd"=> 350,"maxweigh"=>30 )
















for ($i=0; $i<=$maxcoun; $i++)


if ($councode==$countries[$i]["name"])





while ($wtctr<=$weightx)


if ($runwt==$startwt) {$startval=$countries[$i]["val2k"];}

if ($runwt>$startwt and $runwt<=10)



if ($runwt>10 and $runwt<=$maxwt)





if ($runwt>$maxwt)
















return $temsval;





Calling program code:




<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">



<?php require('calcems.php');


print " This is answer: $$joey";





This calling program can be changed anytime. If the input is to be come from user-input, you may do so re-define this calling program. :oops:

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...