Location: PHPKode > scripts > Discount Code module for osCommerce > discount-code-module-for-oscommerce/catalog/includes/modules/order_total/ot_discount.php
<?php
/*
  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License

  Discount Code
  http://high-quality-php-coding.com/
*/

  class ot_discount {
    var $title, $output;

    function ot_discount() {
      $this->code = 'ot_discount';
      $this->title = MODULE_ORDER_TOTAL_DISCOUNT_TITLE;
      $this->description = MODULE_ORDER_TOTAL_DISCOUNT_DESCRIPTION;
      $this->enabled = ((MODULE_ORDER_TOTAL_DISCOUNT_STATUS == 'true') ? true : false);
      $this->sort_order = MODULE_ORDER_TOTAL_DISCOUNT_SORT_ORDER;

      $this->output = array();
    }

    function process() {
      global $order, $currencies, $customer_id, $discount, $sess_discount_code;
      $discount = 0;

      if (!empty($sess_discount_code)) {
        $check_query = tep_db_query(sprintf("select c2dc.discount_codes_id from %s dc, %s c2dc where dc.discount_codes_id = c2dc.discount_codes_id and dc.discount_codes = '%s' and c2dc.customers_id = '%s' limit 1", TABLE_DISCOUNT_CODES, TABLE_CUSTOMERS_TO_DISCOUNT_CODES, tep_db_input($sess_discount_code), $customer_id));
        if (!tep_db_num_rows($check_query)) {
          $check_query = tep_db_query("select dc.products_id, dc.categories_id, dc.orders_total, dc.discount_values from " . TABLE_DISCOUNT_CODES . " dc where dc.discount_codes = '" . tep_db_input($sess_discount_code) . "' and if(dc.expires_date = '0000-00-00', date_format(date_add(now(), interval 1 day), '%Y-%m-%d'), dc.expires_date) >= date_format(now(), '%Y-%m-%d') and dc.minimum_order_amount <= " . $order->info['total'] . " and dc.status = '1' limit 1");
          if (tep_db_num_rows($check_query)) {
            $check = tep_db_fetch_array($check_query);
            if (!empty($check['products_id']) || !empty($check['categories_id'])) {
              $products = array();
              if (!empty($check['products_id'])) {
                $products[] = $check['products_id'];
              } else {
                $product_query = tep_db_query("select p2c.products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p2c.categories_id = '" . (int)$check['categories_id'] . "'");
                while ($product = tep_db_fetch_array($product_query)) {
                  $products[] = $product['products_id'];
                }
              }
              for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
                if (in_array(tep_get_prid($order->products[$i]['id']), $products)) {
                  if (strpos($check['discount_values'], '%') === false) {
                    $discount += $check['discount_values'] * $order->products[$i]['qty'];
                  } else {
                    $discount += $order->products[$i]['final_price'] * str_replace('%', '', $check['discount_values']) / 100 * $order->products[$i]['qty'];
                  }
                }
              }
            } elseif (!empty($check['orders_total'])) {
              if (strpos($check['discount_values'], '%') === false) {
                $discount = $check['discount_values'];
              } else {
                $discount = $order->info['total'] * str_replace('%', '', $check['discount_values']) / 100;
              }
            }
          }
        }
      }

      if (!empty($discount)) {
        $order->info['total'] = $order->info['total'] - $discount;
        $this->output[] = array('title' => TEXT_DISCOUNT . ':',
                                'text' => '<span class="productSpecialPrice">' . $currencies->format($discount, true, $order->info['currency'], $order->info['currency_value']) . '</span>',
                                'value' => $discount);
      }
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_ORDER_TOTAL_DISCOUNT_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }

      return $this->_check;
    }

    function keys() {
      return array('MODULE_ORDER_TOTAL_DISCOUNT_STATUS', 'MODULE_ORDER_TOTAL_DISCOUNT_SORT_ORDER');
    }

    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 Total', 'MODULE_ORDER_TOTAL_DISCOUNT_STATUS', 'true', 'Do you want to display the total order value?', '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_ORDER_TOTAL_DISCOUNT_SORT_ORDER', '4', 'Sort order of display.', '6', '2', now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }
  }
?>
Return current item: Discount Code module for osCommerce