PHP Code:
class zipcodezone {
var $code, $title, $description, $enabled, $num_zones;
// class constructor
function __construct() {
$this->code = 'zipcodezone';
$this->title = MODULE_SHIPPING_ZIPCODEZONE_TEXT_TITLE;
$this->description = MODULE_SHIPPING_ZIPCODEZONE_TEXT_DESCRIPTION;
$this->sort_order = MODULE_SHIPPING_ZIPCODEZONE_SORT_ORDER;
$this->icon = '';
$this->tax_class = MODULE_SHIPPING_ZIPCODEZONE_TAX_CLASS;
$this->tax_basis = MODULE_SHIPPING_ZIPCODEZONE_TAX_BASIS;
// disable only when entire cart is free shipping
if (zen_get_shipping_enabled ($this->code)) {
$this->enabled = (MODULE_SHIPPING_ZIPCODEZONE_STATUS == 'True');
} else {
$this->enabled = false;
}
if (is_object ($order) && $this->enabled && (int)MODULE_SHIPPING_ZIPCODEZONE_ZONE > 0) {
$enabled_for_zone = false;
$check = $db->Execute (
"SELECT zone_id
FROM " . TABLE_ZONES_TO_GEO_ZONES . "
WHERE geo_zone_id = '" . MODULE_SHIPPING_ZIPCODE_ZONE . "'
AND zone_country_id = " . $order->delivery['country']['id'] . "
ORDER BY zone_id"
);
while (!$check->EOF) {
if ($check->fields['zone_id'] < 1 || $check->fields['zone_id'] == $order->delivery['zone_id']) {
$enabled_for_zone = true;
break;
}
$check->MoveNext();
}
$this->enabled = $enabled_for_zone;
}
// CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED
$this->num_zones = 7;
$this->num_zone_start = 2;
if (IS_ADMIN_FLAG === true) {
// build in admin only additional zones if missing in the configuration table due to customization of default $this->num_zones = 3
global $db;
for ($i = $this->num_zone_start; $i <= $this->num_zones + ($this->num_zone_start - 1); $i++) {
$check = $db->Execute("select * from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_ZIPCODEZONE_COUNTRIES_" . $i . "'");
if ($this->enabled && $check->EOF) {
$default_countries = '';
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Zone " . $i ." Countries', 'MODULE_SHIPPING_ZIPCODEZONE_COUNTRIES_" . $i ."', '" . $default_countries . "', 'Comma separated list of two character ISO country codes that are part of Zone " . $i . ".<br />Set as 00 to indicate all two character ISO country codes that are not specifically defined.', '6', '0', 'zen_cfg_textarea(', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Zone " . $i ." Shipping Table', 'MODULE_SHIPPING_ZIPCODEZONE_COST_" . $i ."', '3:8.50,7:10.50,99:20.00', 'Shipping rates to Zone " . $i . " destinations based on a group of maximum order weights/prices. Example: 3:8.50,7:10.50,... Weight/Price less than or equal to 3 would cost 8.50 for Zone " . $i . " destinations.<br />You can end the last amount as 10000:7% to charge 7% of the Order Total', '6', '0', 'zen_cfg_textarea(', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Handling Fee', 'MODULE_SHIPPING_ZIPCODEZONE_HANDLING_" . $i."', '0', 'Handling Fee for this shipping zone', '6', '0', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Handling Per Order or Per Box Zone " . $i . " (when by weight)' , 'MODULE_SHIPPING_ZIPCODEZONE_HANDLING_METHOD_" . $i."', 'Order', 'Do you want to charge Handling Fee Per Order or Per Box?', '6', '0', 'zen_cfg_select_option(array(\'Order\', \'Box\'), ', now())");
}
}
} // build in admin only
if ($this->enabled) {
global $db;
for ($i = $this->num_zone_start; $i <= $this->num_zones + ($this->num_zone_start - 1); $i++) {
// check MODULE_SHIPPING_TABLE_HANDLING_METHOD is in
$check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_ZIPCODEZONE_HANDLING_METHOD_" . $i . "'");
if ($check_query->EOF) {
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Handling Per Order or Per Box Zone " . $i . " (when by weight)' , 'MODULE_SHIPPING_ZIPCODEZONE_HANDLING_METHOD_" . $i."', 'Order', 'Do you want to charge Handling Fee Per Order or Per Box?', '6', '0', 'zen_cfg_select_option(array(\'Order\', \'Box\'), ', now())");
}
}
}
}
// class methods
function quote($method = '') {
global $order, $shipping_weight, $shipping_num_boxes, $total_count;
$dest_country = $order->delivery['country']['iso_code_2'];
$dest_zone = 0;
$error = false;
$order_total_amount = $_SESSION['cart']->show_total() - $_SESSION['cart']->free_shipping_prices() ;
$zipcode_prefix = substr($order->delivery['postcode'],0,3);
global $db;
$check = $db->Execute("select zipcodezone from " . TABLE_ZIPCODEZONE . " where '" . $zipcode_prefix . "' BETWEEN zipcode_start and zipcode_end");
while (!$check->EOF) {
$dest_zone = $check->fields['zipcodezone'];
$check->MoveNext();
}
// echo $dest_zone;
if ($dest_zone == 0) {
$error = true;
} else {
$shipping = -1;
$zones_cost = constant('MODULE_SHIPPING_ZIPCODEZONE_COST_' . $dest_zone);
$zones_table = preg_split("/[:,]/" , $zones_cost);
$size = sizeof($zones_table);
$done = false;
for ($i=0; $i<$size; $i+=2) {
switch (MODULE_SHIPPING_ZIPCODEZONE_METHOD) {
case (MODULE_SHIPPING_ZIPCODEZONE_METHOD == 'Weight'):
if (round($shipping_weight,9) <= $zones_table[$i]) {
$shipping = $zones_table[$i+1];
switch (SHIPPING_BOX_WEIGHT_DISPLAY) {
case (0):
$show_box_weight = '';
break;
case (1):
$show_box_weight = ' (' . $shipping_num_boxes . ' ' . TEXT_SHIPPING_BOXES . ')';
break;
case (2):
$show_box_weight = ' (' . number_format($shipping_weight * $shipping_num_boxes,2) . MODULE_SHIPPING_ZIPCODEZONE_TEXT_UNITS . ')';
break;
default:
$show_box_weight = ' (' . $shipping_num_boxes . ' x ' . number_format($shipping_weight,2) . MODULE_SHIPPING_ZIPCODEZONE_TEXT_UNITS . ')';
break;
}
// $shipping_method = MODULE_SHIPPING_ZIPCODEZONE_TEXT_WAY . ' ' . $dest_country . (SHIPPING_BOX_WEIGHT_DISPLAY >= 2 ? ' : ' . $shipping_weight . ' ' . MODULE_SHIPPING_ZIPCODEZONE_TEXT_UNITS : '');
$shipping_method = MODULE_SHIPPING_ZIPCODEZONE_TEXT_WAY . ' ' . $dest_country . $show_box_weight;
$done = true;
if (strstr($zones_table[$i+1], '%')) {
$shipping = ($zones_table[$i+1]/100) * $order_total_amount;
} else {
$shipping = $zones_table[$i+1];
}
break;
}
break;
case (MODULE_SHIPPING_ZIPCODEZONE_METHOD == 'Price'):
// shipping adjustment
if (($_SESSION['cart']->show_total() - $_SESSION['cart']->free_shipping_prices()) <= $zones_table[$i]) {
$shipping = $zones_table[$i+1];
$shipping_method = MODULE_SHIPPING_ZIPCODEZONE_TEXT_WAY . ' ' . $order->delivery['postcode'];
if (strstr($zones_table[$i+1], '%')) {
$shipping = ($zones_table[$i+1]/100) * $order_total_amount;
} else {
$shipping = $zones_table[$i+1];
}
$done = true;
break;
}
break;
case (MODULE_SHIPPING_ZIPCODEZONE_METHOD == 'Item'):
// shipping adjustment
if (($total_count - $_SESSION['cart']->free_shipping_items()) <= $zones_table[$i]) {
$shipping = $zones_table[$i+1];
$shipping_method = MODULE_SHIPPING_ZIPCODEZONE_TEXT_WAY . ' ' . $order->delivery['postcode'];
$done = true;
if (strstr($zones_table[$i+1], '%')) {
$shipping = ($zones_table[$i+1]/100) * $order_total_amount;
} else {
$shipping = $zones_table[$i+1];
}
break;
}
break;
}
if ($done == true) {
break;
}
}
if ($shipping == -1) {
$shipping_cost = 0;
$shipping_method = MODULE_SHIPPING_ZIPCODEZONE_UNDEFINED_RATE;
} else {
switch (MODULE_SHIPPING_ZIPCODEZONE_METHOD) {
case (MODULE_SHIPPING_ZIPCODEZONE_METHOD == 'Weight'):
// charge per box when done by Weight
// Handling fee per box or order
if (constant('MODULE_SHIPPING_ZIPCODEZONE_HANDLING_METHOD_' . $dest_zone) == 'Box') {
$shipping_cost = ($shipping * $shipping_num_boxes) + constant('MODULE_SHIPPING_ZIPCODEZONE_HANDLING_' . $dest_zone) * $shipping_num_boxes;
} else {
$shipping_cost = ($shipping * $shipping_num_boxes) + constant('MODULE_SHIPPING_ZIPCODEZONE_HANDLING_' . $dest_zone);
}
break;
case (MODULE_SHIPPING_ZIPCODEZONE_METHOD == 'Price'):
// don't charge per box when done by Price
$shipping_cost = ($shipping) + constant('MODULE_SHIPPING_ZIPCODEZONE_HANDLING_' . $dest_zone);
break;
case (MODULE_SHIPPING_ZIPCODEZONE_METHOD == 'Item'):
// don't charge per box when done by Item
$shipping_cost = ($shipping) + constant('MODULE_SHIPPING_ZIPCODEZONE_HANDLING_' . $dest_zone);
break;
}
}
}
$this->quotes = array('id' => $this->code,
'module' => MODULE_SHIPPING_ZIPCODEZONE_TEXT_TITLE,
'methods' => array(array('id' => $this->code,
'title' => $shipping_method,
'cost' => $shipping_cost)));
if ($this->tax_class > 0) {
$this->quotes['tax'] = zen_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
}
if (zen_not_null($this->icon)) $this->quotes['icon'] = zen_image($this->icon, $this->title);
if (strstr(MODULE_SHIPPING_ZIPCODEZONE_SKIPPED, $dest_zone)) {
// don't show anything for this country
$this->quotes = array();
} else {
if ($error == true) $this->quotes['error'] = MODULE_SHIPPING_ZIPCODEZONE_INVALID_ZONE;
}
return $this->quotes;
}
function check() {
global $db;
if (!isset($this->_check)) {
$check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_ZIPCODEZONE_STATUS'");
$this->_check = $check_query->RecordCount();
}
return $this->_check;
}
function install() {
global $db;
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) VALUES ('Enable Zones Method', 'MODULE_SHIPPING_ZIPCODEZONE_STATUS', 'True', 'Do you want to offer zone rate shipping?', '6', '0', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) VALUES ('Calculation Method', 'MODULE_SHIPPING_ZIPCODEZONE_METHOD', 'Weight', 'Calculate cost based on Weight, Price or Item?', '6', '0', 'zen_cfg_select_option(array(\'Weight\', \'Price\', \'Item\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_SHIPPING_ZIPCODEZONE_TAX_CLASS', '0', 'Use the following tax class on the shipping fee.', '6', '0', 'zen_get_tax_class_title', 'zen_cfg_pull_down_tax_classes(', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Tax Basis', 'MODULE_SHIPPING_ZIPCODEZONE_TAX_BASIS', 'Shipping', 'On what basis is Shipping Tax calculated. Options are<br />Shipping - Based on customers Shipping Address<br />Billing Based on customers Billing address<br />Store - Based on Store address if Billing/Shipping Zone equals Store zone', '6', '0', 'zen_cfg_select_option(array(\'Shipping\', \'Billing\', \'Store\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Shipping Zone', 'MODULE_SHIPPING_ZIPCODEZONE_ZONE', '0', 'If a zone is selected, only enable this shipping method for that zone.', '6', '0', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_SHIPPING_ZIPCODEZONE_SORT_ORDER', '0', 'Sort order of display.', '6', '0', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Skip Countries, use a comma separated list of the two character ISO country codes', 'MODULE_SHIPPING_ZIPCODEZONE_SKIPPED', '', 'Disable for the following Countries:', '6', '0', 'zen_cfg_textarea(', now())");
for ($i = $this->num_zone_start; $i <= $this->num_zones + ($this->num_zone_start - 1); $i++) {
$default_countries = '';
if ($i == 1) {
$default_countries = 'US,CA';
}
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Zone " . $i ." Countries', 'MODULE_SHIPPING_ZIPCODEZONE_COUNTRIES_" . $i ."', '" . $default_countries . "', 'Comma separated list of two character ISO country codes that are part of Zone " . $i . ".<br />Set as 00 to indicate all two character ISO country codes that are not specifically defined.', '6', '0', 'zen_cfg_textarea(', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Zone " . $i ." Shipping Table', 'MODULE_SHIPPING_ZIPCODEZONE_COST_" . $i ."', '3:8.50,7:10.50,99:20.00', 'Shipping rates to Zone " . $i . " destinations based on a group of maximum order weights/prices. Example: 3:8.50,7:10.50,... Weight/Price less than or equal to 3 would cost 8.50 for Zone " . $i . " destinations.<br />You can also use percentage amounts, such 25:8.50,35:5%,40:9.50,10000:7% to charge a percentage value of the Order Total', '6', '0', 'zen_cfg_textarea(', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Handling Fee', 'MODULE_SHIPPING_ZIPCODEZONE_HANDLING_" . $i."', '0', 'Handling Fee for this shipping zone', '6', '0', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Handling Per Order or Per Box Zone " . $i . " (when by weight)' , 'MODULE_SHIPPING_ZIPCODEZONE_HANDLING_METHOD_" . $i."', 'Order', 'Do you want to charge Handling Fee Per Order or Per Box?', '6', '0', 'zen_cfg_select_option(array(\'Order\', \'Box\'), ', now())");
}
}
function remove() {
global $db;
$db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key like 'MODULE\_SHIPPING\_ZIPCODEZONE\_%'");
}
function keys() {
$keys = array('MODULE_SHIPPING_ZIPCODEZONE_STATUS', 'MODULE_SHIPPING_ZIPCODEZONE_METHOD', 'MODULE_SHIPPING_ZIPCODEZONE_TAX_CLASS', 'MODULE_SHIPPING_ZIPCODEZONE_TAX_BASIS', 'MODULE_SHIPPING_ZIPCODEZONE_ZONE', 'MODULE_SHIPPING_ZIPCODEZONE_SORT_ORDER', 'MODULE_SHIPPING_ZIPCODEZONE_SKIPPED');
for ($i = $this->num_zone_start; $i <= $this->num_zones + ($this->num_zone_start - 1); $i++) {
$keys[] = 'MODULE_SHIPPING_ZIPCODEZONE_COUNTRIES_' . $i;
$keys[] = 'MODULE_SHIPPING_ZIPCODEZONE_COST_' . $i;
$keys[] = 'MODULE_SHIPPING_ZIPCODEZONE_HANDLING_' . $i;
$keys[] = 'MODULE_SHIPPING_ZIPCODEZONE_HANDLING_METHOD_' . $i;
}
return $keys;
}
}
?>
Bookmarks