zen cart has some plugins that can apply discount based upon product.what if we need to give customer discount based upon the total item number of the order?For example,we need to implement the following discount rules on the zen cart website:
if the total item number is equal 2,then the customer get a discount of 5% (from order total)
when the total item number is bigger than 2, 10% discount apply to whole cart automatically
Both the two discount rule above apply discount based upon whole cart(not single product).You may also want to apply discount based on the order total,see the following two rules:
if the order total is over $400,then the customer get 5% discount (or a fixed discount of $20)
8% discount will apply if the order total is over $1000(also you may just want to apply a fixed discount)
how to implement all of those discount rules?
There are two place show the order total summary info.one is in the zen cart shopping cart page.The other one is the order confirmation page.This is the common case,it depends on the template designer/programmer.
The order total summary on shopping cart page is grab from shopping_cart class located in the file includes/classes/shopping_cart.php.So to implement discount and show discount info in the shopping cart bottom,we need to do the following change:
-------includes/classes/shopping_cart.php
...
...
var $contents;
/**
* shopping cart total price
* @var decimal
*/
var $total;
/**
* shopping cart total weight
* @var decimal
*/
//added by george
var $ototal;
/**
* Method to calculate cart totals(price and weight)
*
* @return void
* @global object access to the db object
*/
function calculate() {
global $db;
$this->total = 0;
$this->weight = 0;
...
...
...
// + or blank adds
if ($attribute_weight->fields['products_attributes_weight_prefix'] == '-') {
$this->weight -= $qty * $new_attributes_weight;
} else {
$this->weight += $qty * $new_attributes_weight;
}
}
} // attributes weight
}
//added by george to apply discount
$this -> ototal = $this -> total; //save origianl order total
if ( $this -> count_contents() == 2) {
$this -> total = $this -> total * 0.95;
} elseif ( $this -> count_contents() >= 3) {
$this -> total = $this -> total * 0.9;
}
//end of discount code
}
/**
* Method to calculate price of attributes for a given item
...
...
/**
* Method to calculate total price of items in cart
*
* @return decimal Total Price
*/
function show_total() {
$this->notify('NOTIFIER_CART_SHOW_TOTAL_START');
$this->calculate();
$this->notify('NOTIFIER_CART_SHOW_TOTAL_END');
return $this->total;
}
//added by george
function show_ototal() {
$this->notify('NOTIFIER_CART_SHOW_TOTAL_START');
$this->calculate();
$this->notify('NOTIFIER_CART_SHOW_TOTAL_END');
return $this->ototal;
}
----includes/modules/pages/shopping_cart/header_php.php
$flagHasCartContents = ($_SESSION['cart']->count_contents() > 0);
$cartShowTotal = $currencies->format($_SESSION['cart']->show_total());
//added by george
$cartOtotal = $currencies->format($_SESSION['cart']->show_ototal());
$discountTotal = $cartOtotal - $cartShowTotal;
----includes/templates/[current_template]/templates/tpl_shopping_cart_default.php
<div id="cartSubTotal"><?php echo SUB_TITLE_ORI_TOTAL;echo $cartOotal;?></div>
<div id="cartSubTotal"><?php echo SUB_TITLE_DIS_TOTAL;echo ''.$discountTotal;?></div>
<div id="cartSubTotal"><?php echo SUB_TITLE_SUB_TOTAL;echo $cartShowTotal;?></div>
The code above all apply and show discount info in the shopping cart,which only save the discount info in session.when user navigate to order confirmation page,zen cart generate order total summary from the order class.so we need to implement discount logic once again:
------includes/classes/order.php
function cart() {
...
...
...
if (isset($GLOBALS[$class]) && is_object($GLOBALS[$class])) {
if ( isset($GLOBALS[$class]->order_status) && is_numeric($GLOBALS[$class]->order_status) && ($GLOBALS[$class]->order_status > 0) ) {
$this->info['order_status'] = $GLOBALS[$class]->order_status;
}
}
//added by george
if ($n == 2) {
$this->info['total'] *= 0.95;
} elseif ($n >=3) {
$this->info['total'] *= 0.9;
}
}
function create($zf_ot_modules, $zf_mode = 2) {
global $db, $zco_notifier;