Code:
<?php
/**
* @package shippingMethod
* @copyright Copyright 2003-2013 Zen Cart Development Team
* @copyright Portions Copyright 2003 osCommerce
* @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
* @version itemcatcan.php 1969 2104-01-03 06:57:21Z ajeh $
*/
class itemcatcan extends base {
/**
* $code determines the internal 'code' name used to designate "this" shipping module
*
* @var string
*/
var $code;
/**
* $title is the displayed name for this shipping method on the storefront
*
* @var string
*/
var $title;
/**
* $description is a soft name for this shipping method, rarely used
*
* @var string
*/
var $description;
/**
* module's icon, if any. Must be manually uploaded to the server's images folder, and an appropriate call to zen_image() added to the constructor.
*
* @var string
*/
var $icon;
/**
* $enabled determines whether this module shows or not during checkout.
* Can be updated with custom code in the module's update_status() method.
* Can be overridden with observers via notifier points NOTIFY_SHIPPING_CHECK_ENABLED_FOR_ZONE and NOTIFY_SHIPPING_CHECK_ENABLED
* @var boolean
*/
var $enabled;
/**
* additional category groups itemcatcan rate
*/
var $num_cat_group;
/**
* constructor
*
* @return itemcatcan
*/
function __construct() {
$this->code = 'itemcatcan';
$this->title = MODULE_SHIPPING_ITEMCATCAN_TEXT_TITLE;
$this->description = MODULE_SHIPPING_ITEMCATCAN_TEXT_DESCRIPTION;
$this->sort_order = MODULE_SHIPPING_ITEMCATCAN_SORT_ORDER;
$this->icon = ''; // add image filename here; must be uploaded to the /images/ subdirectory
$this->tax_class = MODULE_SHIPPING_ITEMCATCAN_TAX_CLASS;
$this->tax_basis = MODULE_SHIPPING_ITEMCATCAN_TAX_BASIS;
$this->enabled = (MODULE_SHIPPING_ITEMCATCAN_STATUS == 'True') ? true : false;
$this->update_status();
$this->notify('MODULE_SHIPPING_' . strtoupper($this->code) . '_INSTANTIATED');
/*
** CUSTOMIZE SETTING FOR THE NUMBER OF CATEGORY GROUPS NEEDED **
If Itemcat Rates module is currently installed ...
1. Change number below for the number of Category Groups for this->num_cat_group
2. Upload changed itemcatcan.php
3. Go to Modules ... Shipping ...
4. Go to Modules ... Shipping ... a second time to clear any error messages
5. Click on Itemcat Rates and Edit and Configure new Category groups
If Itemcat Rates module is not currently installed
1. Change number below for the number of Category Groups for this->num_cat_group
2. Upload changed itemcatcan.php
3. Go to Modules ... Shipping ...
4. Click on Itemcat Rates and Click Install
5. Edit and Configure new Item category groups
*/
$this->num_cat_group = 3;
if (IS_ADMIN_FLAG === true) {
// build in admin only additional category groups if missing in the configuration table due to customization of default $this->num_cat_group = 2
global $db;
for ($i = 1; $i <= $this->num_cat_group; $i++) {
$check = $db->Execute("select * from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_ITEMCATCAN_GROUP_" . $i . "'");
if ($this->enabled && $check->EOF) {
$default_categories_group = '';
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Itemcat group" . $i . " categories', 'MODULE_SHIPPING_ITEMCATCAN_GROUP_" . $i ."', '" . $default_categories_group . "', 'What categories belong to this category group<br />Separate categories by commas, Example: 53,67 or 10, 12', '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 ('Shipping Cost Itemcat group" . $i . "', 'MODULE_SHIPPING_ITEMCATCAN_COST" . $i ."', '" . '2.50' . "', 'The shipping cost will be multiplied by the number of items in an order that uses this Category Group.', '6', '0', now())");
}
}
} // build in admin only
}
/**
* Coders can add custom logic here in the update_status() method to allow for manipulating the $this->enabled status
*/
function update_status() {
global $order, $db;
if (IS_ADMIN_FLAG == TRUE) return;
// disable only when entire cart is free shipping
if (zen_get_shipping_enabled($this->code) == FALSE) $this->enabled = FALSE;
/** CUSTOM ENABLE/DISABLE LOGIC CAN BE ADDED IN THE AREA SPECIFIED BELOW **/
if ($this->enabled) {
global $template, $current_page_base;
// CUSTOMIZED CONDITIONS GO HERE
// Optionally add additional code here to disable the module by changing $this->enabled to false based on whatever custom rules you require.
// -----
// -----
// eof: optional additional code
}
//echo 'ITEM function ' . __FUNCTION__ . ' $this->enabled: ' . ($this->enabled ? ' ON' : ' OFF') . ' $shipping_weight: ' . $shipping_weight . '<br>';
}
/**
* Sets $this->enabled based on zone restrictions applied to this module
* @return boolean
*/
function check_enabled_for_zone()
{
global $order, $db;
if ($this->enabled == true && (int)MODULE_SHIPPING_ITEMCATCAN_ZONE > 0) {
$check_flag = false;
$check = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . "
where geo_zone_id = '" . MODULE_SHIPPING_ITEMCATCAN_ZONE . "'
and zone_country_id = '" . (int)$order->delivery['country']['id'] . "'
order by zone_id");
while (!$check->EOF) {
if ($check->fields['zone_id'] < 1) {
$check_flag = true;
break;
} elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
$check_flag = true;
break;
}
$check->MoveNext();
}
if ($check_flag == false) {
$this->enabled = false;
}
}
return $this->enabled;
}
/**
* Returns the value of $this->enabled variable
* @return boolean
*/
function check_enabled()
{
return $this->enabled;
}
/**
* Obtain quote from shipping system/calculations
*
* @param string $method
* @return array
*/
function quote($method = '') {
global $order, $total_count;
$chk_products_id_list_defined = '';
$chk_total_count = 0;
$chk_free_shipping_total_count = 0;
$chk_products_in_cart = $_SESSION['cart']->get_product_id_list();
//echo '$chk_products_in_cart: ' . $chk_products_in_cart . '<br><br>';
for ($i=1; $i<=$this->num_cat_group; $i++) {
$chk_cat_group = '$chk_cat_group_' . $i;
${$chk_cat_group} = 0;
$cat_group = '$cat_group_' . $i;
$cat_group_constant = constant('MODULE_SHIPPING_ITEMCATCAN_GROUP_' . $i);
${$cat_group} = preg_split("/[,]/" , $cat_group_constant);
$size = sizeof(${$cat_group});
if ($cat_group_constant == '') break; // do not continue if empty
for ($j=0, $n=$size; $j<$n; $j+=1) {
if (${$cat_group}[$j] != '') {
${$chk_cat_group} += $_SESSION['cart']->in_cart_check('master_categories_id', trim(${$cat_group}[$j]));
}
//echo 'Looking at trim(${$cat_group}[$j]): ' . trim(${$cat_group}[$j]) . '<br>';
// check free shipping products
global $categories_products_id_list;
$categories_products_id_list = '';
$chk_products_id_list = array();
$chk_cat_group_products = trim(${$cat_group}[$j]);
$chk_products_id_list = zen_get_categories_products_list($chk_cat_group_products);
//echo '<pre>'; echo var_dump($chk_products_id_list); echo '</pre>';
//echo 'itemcatcan $chk_products_id_list';print_r($chk_products_id_list);
if (is_array($chk_products_id_list) && sizeof($chk_products_id_list) > 0) {
// build products list
$list_of_products = '';
foreach($chk_products_id_list as $key => $value) {
$list_of_products .= $key . ', ';
}
// if any $list_of_products are in cart check if free shipping
$chk_products = trim(trim($list_of_products), ',');
//echo '$chk_products: ' . $chk_products . '<br>in cart $chk_products_in_cart: '. $chk_products_in_cart . '<br>';
$chk_donotchargeshipping = '';
$chk_donotchargeshipping_cnt = 0;
$arr1 = explode(", ", $chk_products_in_cart);
$arr2 = explode(", ", $chk_products);
// echo '<pre>'; echo var_dump($chk_products); echo '</pre>';
$chk_donotchargeshipping = array_intersect($arr1, $arr2);
//echo '<br>itemcatcan $chk_donotchargeshipping<br> '; print_r($chk_donotchargeshipping); echo '<br>';
foreach($chk_donotchargeshipping as $key2 => $value) {
$chk_products_id_list_defined .= $value . ', ';
$chk_donotchargeshipping_cnt = itemcatcan_free_shipping($value);
if ($chk_donotchargeshipping_cnt) {
$chk_free_shipping_total_count += 1;
echo '<br>Do not charge $key2 $value: ' . $value . ' $chk_donotchargeshipping_cnt: ' . $chk_donotchargeshipping_cnt . '<br>';
}
${$chk_cat_group} += -$chk_donotchargeshipping_cnt;
}
//echo 'Cat: ' . trim(${$cat_group}[$j]) . ' ${$chk_cat_group}: ' . ${$chk_cat_group} . '<br>';
}
}
$chk_total_count += ${$chk_cat_group};
$chk_total_charge_constant = constant('MODULE_SHIPPING_ITEMCAT_COST' . $i);
// $chk_total_charge += ($chk_total_charge_constant * ${$chk_cat_group});
$chk_total_charge += (${$chk_cat_group} > 0 ? (1 * $chk_total_charge_constant) + ( (${$chk_cat_group} - 1) * .75) : 0);
}
//echo '$chk_total_count: ' . $chk_total_count . ' $chk_total_charge found: ' . $chk_total_charge . '<br>';
//echo '$chk_products_in_cart: ' . $chk_products_in_cart . '<br>vs $chk_products_id_list_defined: ' . $chk_products_id_list_defined . '<br>';
// check products undefined for Free shipping
//$chk_products_id_list
// if any $list_of_products are in cart check if free shipping
$chk_products_id_list_defined = trim(trim($chk_products_id_list_defined), ',');
//echo '$chk_products: ' . $chk_products . '<br>in cart $chk_products_in_cart: '. $chk_products_in_cart . '<br>';
$chk_donotchargeshipping_undefined = '';
$chk_undefined_free_shipping = 0;
$chk_free_shipping_total_count_undefined = 0;
$arr1 = explode(", ", $chk_products_in_cart);
$arr2 = explode(", ", $chk_products_id_list_defined);
$chk_donotchargeshipping_undefined = array_diff($arr1, $arr2);
foreach($chk_donotchargeshipping_undefined as $key2 => $value) {
// $chk_products_id_list .= $value . ',';
$chk_donotchargeshipping_undefined_cnt = itemcatcan_free_shipping($value);
if ($chk_donotchargeshipping_undefined_cnt) {
echo 'itemcatcan Do not charge UNDEFINED $key2: ' . $value . '<br>';
$chk_free_shipping_total_count_undefined += 1;
}
$chk_undefined_free_shipping += $chk_donotchargeshipping_undefined_cnt;
}
//echo '$chk_undefined_free_shipping: ' . $chk_undefined_free_shipping . '<br>';
// calculate any undefined categories and add to $chk_total_charge
$undefined_products = $_SESSION['cart']->count_contents() - ($chk_total_count + ($chk_free_shipping_total_count + $chk_undefined_free_shipping));
$undefined_products_charge = (MODULE_SHIPPING_ITEMCATCAN_COST_UNDEFINED * ($undefined_products - $chk_undefined_free_shipping));
//echo '$undefined_products: ' . $undefined_products . ' $undefined_products_charge: ' . $undefined_products_charge . '<br>';
$chk_total_charge += $undefined_products_charge;
//echo 'TOTAL CHARGE $chk_total_charge: ' . $chk_total_charge . '<br>';
// adjusted count for free shipping
$item_total_count = $total_count - $_SESSION['cart']->free_shipping_items();
$this->quotes = array('id' => $this->code,
'module' => MODULE_SHIPPING_ITEMCATCAN_TEXT_TITLE,
'methods' => array(array('id' => $this->code,
'title' => MODULE_SHIPPING_ITEMCATCAN_TEXT_WAY,
'cost' => ($chk_total_charge) + MODULE_SHIPPING_ITEMCATCAN_HANDLING)));
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);
$this->notify('MODULE_SHIPPING_' . strtoupper($this->code) . '_QUOTES_PREPARED');
return $this->quotes;
}
/**
* Check to see whether module is installed
*
* @return boolean
*/
function check() {
global $db;
if (!isset($this->_check)) {
$check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_ITEMCATCAN_STATUS'");
$this->_check = $check_query->RecordCount();
}
return $this->_check;
}
/**
* Install the shipping module and its configuration settings
*/
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 Itemcat Shipping', 'MODULE_SHIPPING_ITEMCATCAN_STATUS', 'True', 'Do you want to offer per itemcatcan rate shipping?', '6', '0', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");
for ($i = 1; $i <= $this->num_cat_group; $i++) {
$check = $db->Execute("select * from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_ITEMCATCAN_GROUP_" . $i . "'");
if ($this->enabled && $check->EOF) {
$default_categories_group = '';
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Itemcat group" . $i . " categories', 'MODULE_SHIPPING_ITEMCATCAN_GROUP_" . $i ."', '" . $default_categories_group . "', 'What categories belong to this category group<br />Separate categories by commas, Example: 53,67', '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 ('Shipping Cost Itemcat group" . $i . "', 'MODULE_SHIPPING_ITEMCATCAN_COST" . $i ."', '" . '2.50' . "', 'The shipping cost will be multiplied by the number of items in an order that uses this Category Group.', '6', '0', now())");
}
}
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Shipping Cost for Itemcat category groups undefined', 'MODULE_SHIPPING_ITEMCATCAN_COST_UNDEFINED', '', 'The shipping cost will be multiplied by the number of items in an order that are not in a defined category group.', '6', '0', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Handling Fee', 'MODULE_SHIPPING_ITEMCATCAN_HANDLING', '0', 'Handling fee for this shipping method.', '6', '0', 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_ITEMCATCAN_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_ITEMCATCAN_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_ITEMCATCAN_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_ITEMCATCAN_SORT_ORDER', '0', 'Sort order of display.', '6', '0', now())");
}
/**
* Remove the module and all its settings
*/
function remove() {
global $db;
$db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key like 'MODULE\_SHIPPING\_ITEMCATCAN\_%'");
}
/**
* Internal list of configuration keys used for configuration of the module
*
* @return array
*/
function keys() {
$keys = array('MODULE_SHIPPING_ITEMCATCAN_STATUS', 'MODULE_SHIPPING_ITEMCATCAN_HANDLING', 'MODULE_SHIPPING_ITEMCATCAN_TAX_CLASS', 'MODULE_SHIPPING_ITEMCATCAN_TAX_BASIS', 'MODULE_SHIPPING_ITEMCATCAN_ZONE', 'MODULE_SHIPPING_ITEMCATCAN_SORT_ORDER');
for ($i=1; $i<=$this->num_cat_group; $i++) {
$keys[] = 'MODULE_SHIPPING_ITEMCATCAN_GROUP_' . $i;
$keys[] = 'MODULE_SHIPPING_ITEMCATCAN_COST' . $i;
}
$keys[] = 'MODULE_SHIPPING_ITEMCATCAN_COST_UNDEFINED';
return $keys;
}
}
function itemcatcan_free_shipping($product_id) {
global $db;
if ($product_id == '' || $product_id == 0 || !$product_id) {
// echo 'Blank $product_id<br>';
return 0;
}
if (!zen_has_product_attributes_downloads_status($product_id)) {
//echo 'itemcatcan_free_shipping 1 - A Download: ' . $product_id . ' - ' . zen_has_product_attributes_downloads_status($product_id) . '<br><br>';
return 1;
}
$sql = "SELECT product_is_always_free_shipping, products_virtual, products_model, products_weight from " . TABLE_PRODUCTS . " WHERE products_id = '" . (int)$product_id . "'";
$chk_free_shipping = $db->Execute($sql);
if (($chk_free_shipping->fields['product_is_always_free_shipping'] == 1) or ($chk_free_shipping->fields['products_virtual'] == 1) or (preg_match('/^GIFT/', addslashes($chk_free_shipping->fields['products_model'])))) {
//echo 'itemcatcan_free_shipping 1 - B Virtual: ' . $product_id . '<br><br>';
return 1;
}
if ($chk_free_shipping->fields['products_weight'] == 0 && ORDER_WEIGHT_ZERO_STATUS == 1) {
//echo 'itemcatcan_free_shipping 1 - C Weight: ' . $product_id . '<br><br>';
return 1;
}
return 0;
}
Bookmarks