Welcome Guest, Not a member yet? Register   Sign In
Codeigniter Coupon?
#1

[eluser]blindrc[/eluser]
Has anyone attempted to create a coupon / discount system that works with the Cart class?
#2

[eluser]Amitabh Roy[/eluser]
Well, I did create a discount system for in which discounts could be set for each client from the admin, and discounts were applied at the time of checkout in the front end. I had to extend the cart class to implement the functionality

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));
          // }
     }
     }
        
}


Do note that I did not have to worry about the quantity as the requirement was the customer could buy only one copy of the products at the time of checkout, so the sub total is set to the discounted price itself.

This was for CI 1.7.2




Theme © iAndrew 2016 - Forum software by © MyBB