PHP script for calculating shipping based on weight and destination
Developed this php script for one of my clients eCommerce sites. They needed their shopping cart to calculate shipping costs based on the weight of the item and the destination. And all US shipping was to be Free. Best application for this code is in Click Cart Pro version 6+ but here is the stand alone code if you can benefit from it, go for it. I just ask that the credit stays in the comments. Just enclose the following code in php tags:
/* This script is a modification of the default Calculation On Total Item Weight. Script setup for FREE Shipping to all US delivery locations. All other delivery locations, worldwide, are calculated by Price Groups/Zones and Weight of the product. If the specific delivery location is not specified, the closing part of the script will include a flat rate. */
/* Code mods by themonarch with thanks to fazman at the CCP forums for the basic structure http://forum.kryptronic.com/viewtopic.php?id=18218 and this site for excellent php tutorials http://www.tizag.com */
// Definitions
$info = $this->globals(‘ecom.customship’);
$method = ‘USPS/UPS Standard Shipping’;
$weight = $info['weight'];
// FREE SHIPPING USA ONLY
if ($info['country'] == ‘United States’) {
$custom = array($method => ’0.00′);
// Price Group 1
} elseif (($weight <= ’3′) && ($info['country'] == ‘Canada’ || $info['country'] == ‘Virgin Islands British’ || $info['country'] == ‘Virgin Islands US’)) {
$custom = array($method => ’19.99′);
} elseif (($weight <= ’7′) && ($info['country'] == ‘Canada’ || $info['country'] == ‘Virgin Islands British’ || $info['country'] == ‘Virgin Islands US’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’11′) && ($info['country'] == ‘Canada’ || $info['country'] == ‘Virgin Islands British’ || $info['country'] == ‘Virgin Islands US’)) {
$custom = array($method => ’27.99′);
} elseif (($weight <= ’20′) && ($info['country'] == ‘Canada’ || $info['country'] == ‘Virgin Islands British’ || $info['country'] == ‘Virgin Islands US’)) {
$custom = array($method => ’44.99′);
} elseif (($weight <= ’90′) && ($info['country'] == ‘Canada’ || $info['country'] == ‘Virgin Islands British’ || $info['country'] == ‘Virgin Islands US’)) {
$custom = array($method => ’84.99′);
// Price Group 2
} elseif (($weight <= ’3′) && ($info['country'] == ‘Mexico’)) {
$custom = array($method => ’23.99′);
} elseif (($weight <= ’7′) && ($info['country'] == ‘Mexico’)) {
$custom = array($method => ’34.99′);
} elseif (($weight <= ’11′) && ($info['country'] == ‘Mexico’)) {
$custom = array($method => ’44.99′);
} elseif (($weight <= ’20′) && ($info['country'] == ‘Mexico’)) {
$custom = array($method => ’64.99′);
} elseif (($weight <= ’90′) && ($info['country'] == ‘Mexico’)) {
$custom = array($method => ’118.99′);
// Price Group 3
} elseif (($weight <= ’3′) && ($info['country'] == ‘Australia’ || $info['country'] ==
‘China’ || $info['country'] == ‘Hong Kong’ || $info['country'] == ‘Japan’)) {
$custom = array($method => ’31.99′);
} elseif (($weight <= ’7′) && ($info['country'] == ‘Australia’ || $info['country'] ==
‘China’ || $info['country'] == ‘Hong Kong’ || $info['country'] == ‘Japan’)) {
$custom = array($method => ’48.99′);
} elseif (($weight <= ’11′) && ($info['country'] == ‘Australia’ || $info['country'] ==
‘China’ || $info['country'] == ‘Hong Kong’ || $info['country'] == ‘Japan’)) {
$custom = array($method => ’51.99′);
} elseif (($weight <= ’20′) && ($info['country'] == ‘Australia’ || $info['country'] ==
‘China’ || $info['country'] == ‘Hong Kong’ || $info['country'] == ‘Japan’)) {
$custom = array($method => ’92.99′);
} elseif (($weight <= ’91′) && ($info['country'] == ‘Australia’ || $info['country'] ==
‘China’ || $info['country'] == ‘Hong Kong’ || $info['country'] == ‘Japan’)) {
$custom = array($method => ’186.99′);
// Price Group 4
} elseif (($weight <= ’3′) && ($info['country'] == ‘Czech Republic’ || $info['country'] == ‘Hungary’ || $info['country'] == ‘Romania’ || $info['country'] == ‘Poland’)) {
$custom = array($method => ’27.99′);
} elseif (($weight <= ’7′) && ($info['country'] == ‘Czech Republic’ || $info['country'] == ‘Hungary’ || $info['country'] == ‘Romania’ || $info['country'] == ‘Poland’)) {
$custom = array($method => ’39.99′);
} elseif (($weight <= ’11′) && ($info['country'] == ‘Czech Republic’ || $info['country'] == ‘Hungary’ || $info['country'] == ‘Romania’ || $info['country'] == ‘Poland’)) {
$custom = array($method => ’44.99′);
} elseif (($weight <= ’20′) && ($info['country'] == ‘Czech Republic’ || $info['country'] == ‘Hungary’ || $info['country'] == ‘Romania’ || $info['country'] == ‘Poland’)) {
$custom = array($method => ’84.99′);
} elseif (($weight <= ’91′) && ($info['country'] == ‘Czech Republic’ || $info['country'] == ‘Hungary’ || $info['country'] == ‘Romania’ || $info['country'] == ‘Poland’)) {
$custom = array($method => ’178.99′);
// Price Group 5
} elseif (($weight <= ’3′) && ($info['country'] == ‘Austria’ || $info['country'] == ‘Belgium’ || $info['country'] == ‘Denmark’ || $info['country'] == ‘Finland’ || $info['country'] == ‘France’ || $info['country'] == ‘Germany’ || $info['country'] == ‘United Kingdom’ || $info['country'] == ‘Greece’ || $info['country'] == ‘Ireland’ || $info['country'] == ‘Netherlands’ || $info['country'] == ‘Norway’ || $info['country'] == ‘Spain’ || $info['country'] == ‘Sweden’ || $info['country'] == ‘Liechtenstein’ || $info['country'] == ‘Switzerland’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’7′) && ($info['country'] == ‘Austria’ || $info['country'] == ‘Belgium’ || $info['country'] == ‘Denmark’ || $info['country'] == ‘Finland’ || $info['country'] == ‘France’ || $info['country'] == ‘Germany’ || $info['country'] == ‘United Kingdom’ || $info['country'] == ‘Greece’ || $info['country'] == ‘Ireland’ || $info['country'] == ‘Netherlands’ || $info['country'] == ‘Norway’ || $info['country'] == ‘Spain’ || $info['country'] == ‘Sweden’ || $info['country'] == ‘Liechtenstein’ || $info['country'] == ‘Switzerland’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’11′) && ($info['country'] == ‘Austria’ || $info['country'] == ‘Belgium’ || $info['country'] == ‘Denmark’ || $info['country'] == ‘Finland’ || $info['country'] == ‘France’ || $info['country'] == ‘Germany’ || $info['country'] == ‘United Kingdom’ || $info['country'] == ‘Greece’ || $info['country'] == ‘Ireland’ || $info['country'] == ‘Netherlands’ || $info['country'] == ‘Norway’ || $info['country'] == ‘Spain’ || $info['country'] == ‘Sweden’ || $info['country'] == ‘Liechtenstein’ || $info['country'] == ‘Switzerland’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’20′) && ($info['country'] == ‘Austria’ || $info['country'] == ‘Belgium’ || $info['country'] == ‘Denmark’ || $info['country'] == ‘Finland’ || $info['country'] == ‘France’ || $info['country'] == ‘Germany’ || $info['country'] == ‘United Kingdom’ || $info['country'] == ‘Greece’ || $info['country'] == ‘Ireland’ || $info['country'] == ‘Netherlands’ || $info['country'] == ‘Norway’ || $info['country'] == ‘Spain’ || $info['country'] == ‘Sweden’ || $info['country'] == ‘Liechtenstein’ || $info['country'] == ‘Switzerland’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’91′) && ($info['country'] == ‘Austria’ || $info['country'] == ‘Belgium’ || $info['country'] == ‘Denmark’ || $info['country'] == ‘Finland’ || $info['country'] == ‘France’ || $info['country'] == ‘Germany’ || $info['country'] == ‘United Kingdom’ || $info['country'] == ‘Greece’ || $info['country'] == ‘Ireland’ || $info['country'] == ‘Netherlands’ || $info['country'] == ‘Norway’ || $info['country'] == ‘Spain’ || $info['country'] == ‘Sweden’ || $info['country'] == ‘Liechtenstein’ || $info['country'] == ‘Switzerland’)) {
$custom = array($method => ’25.99′);
// Price Group 6
} elseif (($weight <= ’3′) && ($info['country'] == ‘India’ || $info['country'] == ‘Indonesia’ || $info['country'] == ‘New Zealand’ || $info['country'] == ‘Singapore’ || $info['country'] == ‘Taiwan’ || $info['country'] == ‘Vietnam’ || $info['country'] == ‘Thailand’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’7′) && ($info['country'] == ‘India’ || $info['country'] == ‘Indonesia’ || $info['country'] == ‘New Zealand’ || $info['country'] == ‘Singapore’ || $info['country'] == ‘Taiwan’ || $info['country'] == ‘Vietnam’ || $info['country'] == ‘Thailand’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’11′) && ($info['country'] == ‘India’ || $info['country'] == ‘Indonesia’ || $info['country'] == ‘New Zealand’ || $info['country'] == ‘Singapore’ || $info['country'] == ‘Taiwan’ || $info['country'] == ‘Vietnam’ || $info['country'] == ‘Thailand’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’20′) && ($info['country'] == ‘India’ || $info['country'] == ‘Indonesia’ || $info['country'] == ‘New Zealand’ || $info['country'] == ‘Singapore’ || $info['country'] == ‘Taiwan’ || $info['country'] == ‘Vietnam’ || $info['country'] == ‘Thailand’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’91′) && ($info['country'] == ‘India’ || $info['country'] == ‘Indonesia’ || $info['country'] == ‘New Zealand’ || $info['country'] == ‘Singapore’ || $info['country'] == ‘Taiwan’ || $info['country'] == ‘Vietnam’ || $info['country'] == ‘Thailand’)) {
$custom = array($method => ’25.99′);
// Price Group 7
} elseif (($weight <= ’3′) && ($info['country'] == ‘South Africa’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’7′) && ($info['country'] == ‘South Africa’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’11′) && ($info['country'] == ‘South Africa’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’20′) && ($info['country'] == ‘South Africa’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’91′) && ($info['country'] == ‘South Africa’)) {
$custom = array($method => ’25.99′);
// Price Group 8
} elseif (($weight <= ’3′) && ($info['country'] == ‘Egypt’ || $info['country'] ==
‘Israel’ || $info['country'] == ‘Saudi Arabia’ || $info['country'] == ‘United Arab Emirates’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’7′) && ($info['country'] == ‘Egypt’ || $info['country'] ==
‘Israel’ || $info['country'] == ‘Saudi Arabia’ || $info['country'] == ‘United Arab Emirates’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’11′) && ($info['country'] == ‘Egypt’ || $info['country'] ==
‘Israel’ || $info['country'] == ‘Saudi Arabia’ || $info['country'] == ‘United Arab Emirates’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’20′) && ($info['country'] == ‘Egypt’ || $info['country'] ==
‘Israel’ || $info['country'] == ‘Saudi Arabia’ || $info['country'] == ‘United Arab Emirates’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’91′) && ($info['country'] == ‘Egypt’ || $info['country'] ==
‘Israel’ || $info['country'] == ‘Saudi Arabia’ || $info['country'] == ‘United Arab Emirates’)) {
$custom = array($method => ’25.99′);
// Price Group 9
} elseif (($weight <= ’3′) && ($info['country'] == ‘Argentina’ || $info['country'] ==
‘Bahamas’ || $info['country'] == ‘Bermuda’ || $info['country'] == ‘Brazil’ || $info['country'] == ‘Chile’
|| $info['country'] == ‘Colombia’ || $info['country'] == ‘Jamaica’ || $info['country'] == ‘Panama’ || $info['country'] == ‘Peru’ || $info['country'] == ‘Venezuela’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’7′) && ($info['country'] == ‘Argentina’ || $info['country'] ==
‘Bahamas’ || $info['country'] == ‘Bermuda’ || $info['country'] == ‘Brazil’ || $info['country'] == ‘Chile’
|| $info['country'] == ‘Colombia’ || $info['country'] == ‘Jamaica’ || $info['country'] == ‘Panama’ || $info['country'] == ‘Peru’ || $info['country'] == ‘Venezuela’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’11′) && ($info['country'] == ‘Argentina’ || $info['country'] ==
‘Bahamas’ || $info['country'] == ‘Bermuda’ || $info['country'] == ‘Brazil’ || $info['country'] == ‘Chile’
|| $info['country'] == ‘Colombia’ || $info['country'] == ‘Jamaica’ || $info['country'] == ‘Panama’ || $info['country'] == ‘Peru’ || $info['country'] == ‘Venezuela’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’20′) && ($info['country'] == ‘Argentina’ || $info['country'] ==
‘Bahamas’ || $info['country'] == ‘Bermuda’ || $info['country'] == ‘Brazil’ || $info['country'] == ‘Chile’
|| $info['country'] == ‘Colombia’ || $info['country'] == ‘Jamaica’ || $info['country'] == ‘Panama’ || $info['country'] == ‘Peru’ || $info['country'] == ‘Venezuela’)) {
$custom = array($method => ’25.99′);
} elseif (($weight <= ’91′) && ($info['country'] == ‘Argentina’ || $info['country'] ==
‘Bahamas’ || $info['country'] == ‘Bermuda’ || $info['country'] == ‘Brazil’ || $info['country'] == ‘Chile’
|| $info['country'] == ‘Colombia’ || $info['country'] == ‘Jamaica’ || $info['country'] == ‘Panama’ || $info['country'] == ‘Peru’ || $info['country'] == ‘Venezuela’)) {
$custom = array($method => ’25.99′);
// Default Shipping Cost Any Other Country
} else {
$custom = array($method => ’55.00′);
}
// Closing Returns
$this->globals(‘ecom.customship_response’,$custom);








Want to Leave a Reply?