Code:
class MY_Cart extends CI_Cart
{
var $_pre_discount_total = '';
function __construct()
{
parent::CI_Cart();
}
/* function testCartSystem()
{
print_r($this->_cart_contents);
}*/
#get original Total without the discounts
function getOriginalTotal()
{
$original_total = 0;
$cart = $this->_cart_contents;
// Remove these so they don't create a problem when showing the cart table
unset($cart['total_items']);
unset($cart['cart_total']);
foreach($cart as $key => $currProduct){
$original_total += $currProduct['old_subtotal'] <> ''?$currProduct['old_subtotal']:$currProduct['price'];
}
return $original_total;
}
/**
* set the discounts for each product according to the discount type
* @param $productUniqueId the unique rowid set for the product by the Codeigniter cart
* @param $productId the normal product Id passed by us
* @param $prodDiscountArr contains the discount details for the product for the client, specifically the discount type and the discount amount
*
*/
function setProductDiscounts($productUniqueId,$productId,$prodDiscountArr)
{
#if the discount is not set for the given product, calculate and set the discount
if(!array_key_exists('discount_set',$this->_cart_contents["$productUniqueId"])){
//if(!$this->_cart_contents["$productUniqueId"]['discount_set'])
// {
if($prodDiscountArr->discount_type == 'flat_amount'){
$currentOldPrice = $this->_cart_contents["$productUniqueId"]['price'];
$newPrice = $currentOldPrice - $prodDiscountArr->amount;
}elseif($prodDiscountArr->discount_type == 'percentage_discount'){
$currentOldPrice = $this->_cart_contents["$productUniqueId"]['price'];
$newPrice = $currentOldPrice - ($prodDiscountArr->amount/100)*$currentOldPrice;
}elseif($prodDiscountArr->discount_type == 'new_fixed_price'){
$currentOldPrice = $this->_cart_contents["$productUniqueId"]['price'];
$newPrice = $prodDiscountArr->amount;
}
#set the new discount for the product
$this->_cart_contents["$productUniqueId"]['old_subtotal'] = $this->_cart_contents["$productUniqueId"]['price'];
$this->_cart_contents["$productUniqueId"]['price'] = $newPrice;
$this->_cart_contents["$productUniqueId"]['subtotal'] = $newPrice;
$this->_cart_contents["$productUniqueId"]['discount_set'] = 1;
$this->_cart_contents['cart_total'] = $this->_cart_contents['cart_total'] - $currentOldPrice + $newPrice;
// If we made it this far it means that our cart has data.Let's pass it to the Session class so it can be stored
$this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));
// }
}
}
}