Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Need help with order total module


Guest

Recommended Posts

Hi all!!!

 

REALLY need some help!

 

I am working on a new order total modual for my store and am having a heap of problems.

 

I want to give a discount if the customer is subscribed to our newsletter on an ongoing basis.

 

I am trying to do this by using the following querry:

 

$newsletter_query = tep_db_query("select customers_newsletter from " . TABLE_CUSTOMERS . " where customers_id = '" . $customer_id . "'");

$newsletter = tep_db_fetch_array($newsletter_query);

if ($newsletter['customers_newsletter'] > 0) {

 

problem is it doesn't seem to work... any ideas?

 

Any help much appreciated!!!

Cheers,

 

Ed.

 

 

P.S. Here is my complete file!

 

 

 

<?php

/*

$Id: ot_news_discount.php,v 1.41 2004-09-14 dreamscape Exp $

 

osCommerce, Open Source E-Commerce Solutions

http://www.oscommerce.com

 

Copyright © 2004 Josh Dechant

Protions Copyright © 2003 osCommerce

 

Released under the GNU General Public License

*/

 

class ot_news_discount {

var $title, $output;

 

function ot_news_discount() {

$this->code = 'ot_news_discount';

$this->title = MODULE_NEWS_DISCOUNT_TITLE;

$this->description = MODULE_NEWS_DISCOUNT_DESCRIPTION;

$this->enabled = MODULE_NEWS_DISCOUNT_STATUS;

$this->sort_order = MODULE_NEWS_DISCOUNT_SORT_ORDER;

$this->include_shipping = MODULE_NEWS_DISCOUNT_INC_SHIPPING;

$this->include_tax = MODULE_NEWS_DISCOUNT_INC_TAX;

$this->calculate_tax = MODULE_NEWS_DISCOUNT_CALC_TAX;

$this->output = array();

}

 

function process() {

$newsletter_query = tep_db_query("select customers_newsletter from " . TABLE_CUSTOMERS . " where customers_id = '" . $customer_id . "'");

$newsletter = tep_db_fetch_array($newsletter_query);

if ($newsletter['customers_newsletter'] > 0) {

 

 

global $order, $currencies, $ot_subtotal;

 

$od_amount = $this->calculate_discount($this->get_order_total());

if ($this->calculate_tax == 'true') $tod_amount = $this->calculate_tax_effect($od_amount);

 

if ($od_amount > 0) {

if (MODULE_NEWS_DISCOUNT_RATE_TYPE == 'percentage') $title_ext = sprintf(MODULE_NEWS_DISCOUNT_PERCENTAGE_TEXT_EXTENSION ,$this->calculate_rate($_SESSION['cart']->count_contents()));

$this->deduction = $od_amount+$tod_amount;

$this->output[] = array('title' => sprintf(MODULE_NEWS_DISCOUNT_FORMATED_TITLE, $title_ext),

'text' => sprintf(MODULE_NEWS_DISCOUNT_FORMATED_TEXT, $currencies->format($od_amount)),

'value' => $od_amount);

$order->info['total'] -= $this->deduction;

$order->info['tax'] -= $tod_amount;

if ($this->sort_order < $ot_subtotal->sort_order) $order->info['subtotal'] -= $this->deduction;

 

}

}

}

function calculate_discount($amount) {

global $news_discount, $order_total_array;

 

$od_amount = 0;

if ((MODULE_NEWS_DISCOUNT_DISABLE_WITH_COUPON == 'true') && (isset($_SESSION['cc_id']))) return $od_amount;

 

$news_discount = $this->calculate_rate($_SESSION['cart']->count_contents());

if ($news_discount > 0) {

if (MODULE_NEWS_DISCOUNT_RATE_TYPE == 'percentage') {

$od_amount = round((($amount*10)/10)*($news_discount/100), 2);

} else {

$od_amount = round((($news_discount*10)/10), 2);

}

}

 

return $od_amount;

}

 

function calculate_rate($order_qty) {

$discount_rate = split("[:,]" , MODULE_NEWS_DISCOUNT_RATES);

$size = sizeof($discount_rate);

for ($i=0, $n=$size; $i<$n; $i+=2) {

if ($order_qty >= $discount_rate[$i]) {

$news_discount = $discount_rate[$i+1];

}

}

 

return $news_discount;

}

 

function calculate_tax_effect($od_amount) {

global $order;

 

if (MODULE_NEWS_DISCOUNT_RATE_TYPE == 'percentage') {

$tod_amount = 0;

reset($order->info['tax_groups']);

while (list($key, $value) = each($order->info['tax_groups'])) {

$god_amount = 0;

$tax_rate = tep_get_tax_rate_from_desc($key);

$net = ($tax_rate * $order->info['tax_groups'][$key]);

if ($net > 0) {

$god_amount = $this->calculate_discount($order->info['tax_groups'][$key]);

$tod_amount += $god_amount;

$order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;

}

}

} else {

$tod_amount = 0;

reset($order->info['tax_groups']);

while (list($key, $value) = each($order->info['tax_groups'])) {

$god_amount = 0;

$tax_rate = tep_get_tax_rate_from_desc($key);

$net = ($tax_rate * $order->info['tax_groups'][$key]);

if ($net>0) {

$god_amount = ($tax_rate/100)*$od_amount;

$tod_amount += $god_amount;

$order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;

}

}

}

 

return $tod_amount;

}

 

function get_order_total() {

global $order;

 

$order_total = $order->info['total'];

if ($this->include_tax == 'false') $order_total = ($order_total - $order->info['tax']);

if ($this->include_shipping == 'false') $order_total = ($order_total - $order->info['shipping_cost']);

return $order_total;

}

 

function check() {

if (!isset($this->check)) {

$check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_NEWS_DISCOUNT_STATUS'");

$this->check = mysql_num_rows($check_query);

}

 

return $this->check;

}

 

function keys() {

return array('MODULE_NEWS_DISCOUNT_STATUS', 'MODULE_NEWS_DISCOUNT_SORT_ORDER', 'MODULE_NEWS_DISCOUNT_DISABLE_WITH_COUPON', 'MODULE_NEWS_DISCOUNT_RATE_TYPE', 'MODULE_NEWS_DISCOUNT_RATES', 'MODULE_NEWS_DISCOUNT_INC_SHIPPING', 'MODULE_NEWS_DISCOUNT_INC_TAX', 'MODULE_NEWS_DISCOUNT_CALC_TAX');

}

 

function install() {

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display Quantity Discount', 'MODULE_NEWS_DISCOUNT_STATUS', 'true', 'Do you want to enable the quantity discount module?', '6', '1','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_NEWS_DISCOUNT_SORT_ORDER', '2', 'Sort order of display.', '6', '2', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Disable If Coupon Used', 'MODULE_NEWS_DISCOUNT_DISABLE_WITH_COUPON', 'true', 'Do you want to disable the quantity discount module if a discount coupon is being used by the user?', '6', '3','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Discount Rate Type', 'MODULE_NEWS_DISCOUNT_RATE_TYPE', 'percentage', 'Choose the type of discount rate - percentage or flat rate', '6', '4','tep_cfg_select_option(array(\'percentage\', \'flat rate\'), ', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Discount Rates', 'MODULE_NEWS_DISCOUNT_RATES', '10:5,20:10', 'The discount is based on the total number of items. Example: 10:5,20:10.. 10 or more items get a 5% or $5 discount; 20 or more items receive a 10% or $10 disount; depending on the rate type.', '6', '5', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Include Shipping', 'MODULE_NEWS_DISCOUNT_INC_SHIPPING', 'false', 'Include Shipping in calculation', '6', '6', 'tep_cfg_select_option(array(\'true\', \'false\'), ', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Include Tax', 'MODULE_NEWS_DISCOUNT_INC_TAX', 'false', 'Include Tax in calculation.', '6', '7','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Calculate Tax', 'MODULE_NEWS_DISCOUNT_CALC_TAX', 'true', 'Re-calculate Tax on discounted amount.', '6', '8','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");

}

 

function remove() {

tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");

}

}

 

////

// Get tax rate from tax description

if (!function_exists(tep_get_tax_rate_from_desc)) {

function tep_get_tax_rate_from_desc($tax_desc) {

$tax_query = tep_db_query("select tax_rate from " . TABLE_TAX_RATES . " where tax_description = '" . $tax_desc . "'");

$tax = mysql_fetch_assoc($tax_query);

return $tax['tax_rate'];

}

}

?>

Link to comment
Share on other sites

try this, replace the function process()

 

function process() {
global $order, $currencies, $ot_subtotal, $customer_id;

$newsletter_query = tep_db_query("select customers_newsletter from " . TABLE_CUSTOMERS . " where customers_id = '" . $customer_id . "'");
if (($newsletter = tep_db_fetch_array($newsletter_query)) &&
$newsletter['customers_newsletter'] == 1) {


$od_amount = $this->calculate_discount($this->get_order_total());
if ($this->calculate_tax == 'true') $tod_amount = $this->calculate_tax_effect($od_amount);

if ($od_amount > 0) {
if (MODULE_NEWS_DISCOUNT_RATE_TYPE == 'percentage') $title_ext = sprintf(MODULE_NEWS_DISCOUNT_PERCENTAGE_TEXT_EXTENSION ,$this->calculate_rate($_SESSION['cart']->count_contents()));
$this->deduction = $od_amount+$tod_amount;
$this->output[] = array('title' => sprintf(MODULE_NEWS_DISCOUNT_FORMATED_TITLE, $title_ext),
'text' => sprintf(MODULE_NEWS_DISCOUNT_FORMATED_TEXT, $currencies->format($od_amount)),
'value' => $od_amount);
$order->info['total'] -= $this->deduction;
$order->info['tax'] -= $tod_amount;
if ($this->sort_order < $ot_subtotal->sort_order) $order->info['subtotal'] -= $this->deduction;

}
}
}

 

probably still aint gonna fully work because you have other undefined variables like $title_ext. Make sure you initialize them or declare them before use.

Link to comment
Share on other sites

Hi!!!

 

Thanks so much for your help!!!

 

However!!! I'm not sure this has worked. I made the adjustment as you suggested but not the OT modual doesn't show up in the admin!

 

Any idea as to how i could fix this!

 

Thanks Heaps!!!

 

Edwin.

Link to comment
Share on other sites

It was a stupid mistake on my part (copied the languages file to the wrong directory)...

 

However the module still does not work...

 

I have tried all of the following:

 

$newsletter['customers_newsletter'] == 1) {

 

$newsletter['customers_newsletter'] == '1') {

 

$newsletter['customers_newsletter'] == '0') {

 

$newsletter['customers_newsletter'] > 0) {

 

$newsletter['customers_newsletter'] > '0') {

 

 

Currently my file looks like this!

 

<?php

/*

$Id: ot_news_discount.php,v 1.41 2004-09-14 dreamscape Exp $

 

osCommerce, Open Source E-Commerce Solutions

http://www.oscommerce.com

 

Copyright ? 2004 Josh Dechant

Protions Copyright ? 2003 osCommerce

 

Released under the GNU General Public License

*/

 

class ot_news_discount {

var $title, $output;

 

function ot_news_discount() {

$this->code = 'ot_news_discount';

$this->title = MODULE_NEWS_DISCOUNT_TITLE;

$this->description = MODULE_NEWS_DISCOUNT_DESCRIPTION;

$this->enabled = MODULE_NEWS_DISCOUNT_STATUS;

$this->sort_order = MODULE_NEWS_DISCOUNT_SORT_ORDER;

$this->include_shipping = MODULE_NEWS_DISCOUNT_INC_SHIPPING;

$this->include_tax = MODULE_NEWS_DISCOUNT_INC_TAX;

$this->calculate_tax = MODULE_NEWS_DISCOUNT_CALC_TAX;

$this->output = array();

}

 

function process() {

global $order, $currencies, $ot_subtotal, $customer_id;

 

$newsletter_query = tep_db_query("select customers_newsletter from " . TABLE_CUSTOMERS . " where customers_id = '" . $customer_id . "'");

if (($newsletter = tep_db_fetch_array($newsletter_query)) &&

$newsletter['customers_newsletter'] == 1) {

 

 

$od_amount = $this->calculate_discount($this->get_order_total());

if ($this->calculate_tax == 'true') $tod_amount = $this->calculate_tax_effect($od_amount);

 

if ($od_amount > 0) {

if (MODULE_NEWS_DISCOUNT_RATE_TYPE == 'percentage') $title_ext = sprintf(MODULE_NEWS_DISCOUNT_PERCENTAGE_TEXT_EXTENSION ,$this->calculate_rate($_SESSION['cart']->count_contents()));

$this->deduction = $od_amount+$tod_amount;

$this->output[] = array('title' => sprintf(MODULE_NEWS_DISCOUNT_FORMATED_TITLE, $title_ext),

'text' => sprintf(MODULE_NEWS_DISCOUNT_FORMATED_TEXT, $currencies->format($od_amount)),

'value' => $od_amount);

$order->info['total'] -= $this->deduction;

$order->info['tax'] -= $tod_amount;

if ($this->sort_order < $ot_subtotal->sort_order) $order->info['subtotal'] -= $this->deduction;

 

}

}

}

 

function calculate_discount($amount) {

global $news_discount, $order_total_array;

 

$od_amount = 0;

if ((MODULE_NEWS_DISCOUNT_DISABLE_WITH_COUPON == 'true') && (isset($_SESSION['cc_id']))) return $od_amount;

 

$news_discount = $this->calculate_rate($_SESSION['cart']->count_contents());

if ($news_discount > 0) {

if (MODULE_NEWS_DISCOUNT_RATE_TYPE == 'percentage') {

$od_amount = round((($amount*10)/10)*($news_discount/100), 2);

} else {

$od_amount = round((($news_discount*10)/10), 2);

}

}

 

return $od_amount;

}

 

function calculate_rate($order_qty) {

$discount_rate = split("[:,]" , MODULE_NEWS_DISCOUNT_RATES);

$size = sizeof($discount_rate);

for ($i=0, $n=$size; $i<$n; $i+=2) {

if ($order_qty >= $discount_rate[$i]) {

$news_discount = $discount_rate[$i+1];

}

}

 

return $news_discount;

}

 

function calculate_tax_effect($od_amount) {

global $order;

 

if (MODULE_NEWS_DISCOUNT_RATE_TYPE == 'percentage') {

$tod_amount = 0;

reset($order->info['tax_groups']);

while (list($key, $value) = each($order->info['tax_groups'])) {

$god_amount = 0;

$tax_rate = tep_get_tax_rate_from_desc($key);

$net = ($tax_rate * $order->info['tax_groups'][$key]);

if ($net > 0) {

$god_amount = $this->calculate_discount($order->info['tax_groups'][$key]);

$tod_amount += $god_amount;

$order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;

}

}

} else {

$tod_amount = 0;

reset($order->info['tax_groups']);

while (list($key, $value) = each($order->info['tax_groups'])) {

$god_amount = 0;

$tax_rate = tep_get_tax_rate_from_desc($key);

$net = ($tax_rate * $order->info['tax_groups'][$key]);

if ($net>0) {

$god_amount = ($tax_rate/100)*$od_amount;

$tod_amount += $god_amount;

$order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;

}

}

}

 

return $tod_amount;

}

 

function get_order_total() {

global $order;

 

$order_total = $order->info['total'];

if ($this->include_tax == 'false') $order_total = ($order_total - $order->info['tax']);

if ($this->include_shipping == 'false') $order_total = ($order_total - $order->info['shipping_cost']);

return $order_total;

}

 

function check() {

if (!isset($this->check)) {

$check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_NEWS_DISCOUNT_STATUS'");

$this->check = mysql_num_rows($check_query);

}

 

return $this->check;

}

 

function keys() {

return array('MODULE_NEWS_DISCOUNT_STATUS', 'MODULE_NEWS_DISCOUNT_SORT_ORDER', 'MODULE_NEWS_DISCOUNT_DISABLE_WITH_COUPON', 'MODULE_NEWS_DISCOUNT_RATE_TYPE', 'MODULE_NEWS_DISCOUNT_RATES', 'MODULE_NEWS_DISCOUNT_INC_SHIPPING', 'MODULE_NEWS_DISCOUNT_INC_TAX', 'MODULE_NEWS_DISCOUNT_CALC_TAX');

}

 

function install() {

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display Quantity Discount', 'MODULE_NEWS_DISCOUNT_STATUS', 'true', 'Do you want to enable the quantity discount module?', '6', '1','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_NEWS_DISCOUNT_SORT_ORDER', '2', 'Sort order of display.', '6', '2', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Disable If Coupon Used', 'MODULE_NEWS_DISCOUNT_DISABLE_WITH_COUPON', 'true', 'Do you want to disable the quantity discount module if a discount coupon is being used by the user?', '6', '3','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Discount Rate Type', 'MODULE_NEWS_DISCOUNT_RATE_TYPE', 'percentage', 'Choose the type of discount rate - percentage or flat rate', '6', '4','tep_cfg_select_option(array(\'percentage\', \'flat rate\'), ', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Discount Rates', 'MODULE_NEWS_DISCOUNT_RATES', '10:5,20:10', 'The discount is based on the total number of items. Example: 10:5,20:10.. 10 or more items get a 5% or $5 discount; 20 or more items receive a 10% or $10 disount; depending on the rate type.', '6', '5', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Include Shipping', 'MODULE_NEWS_DISCOUNT_INC_SHIPPING', 'false', 'Include Shipping in calculation', '6', '6', 'tep_cfg_select_option(array(\'true\', \'false\'), ', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Include Tax', 'MODULE_NEWS_DISCOUNT_INC_TAX', 'false', 'Include Tax in calculation.', '6', '7','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Calculate Tax', 'MODULE_NEWS_DISCOUNT_CALC_TAX', 'true', 'Re-calculate Tax on discounted amount.', '6', '8','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");

}

 

function remove() {

tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");

}

}

 

////

// Get tax rate from tax description

if (!function_exists(tep_get_tax_rate_from_desc)) {

function tep_get_tax_rate_from_desc($tax_desc) {

$tax_query = tep_db_query("select tax_rate from " . TABLE_TAX_RATES . " where tax_description = '" . $tax_desc . "'");

$tax = mysql_fetch_assoc($tax_query);

return $tax['tax_rate'];

}

}

?>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...