Skip to content

Cart Class Tax and Discount

tutkun edited this page Sep 27, 2014 · 9 revisions

This is an extension to the Codeigniter Cart class. I've seen a lot of people looking for things like tax and discount abilities. I have included two variables for a flat shipping rate, but the code has not been added as I did not need it for the project I wrote it for, however based off the code included in the MY_Cart.php file this is fairly easy to add by looking at how the tax was applied.

The discount works by both quantity and total items. You could even add another variable to make these use two different variables if you wanted to.

First create a configuration file with the following contents.

You can call this whatever you like, but I called it payment_config.php

<?php
if (!defined("BASEPATH")) exit("No direct script access allowed");
$config['discount_multiple_items'] = '2';
$config['discount_multi_percent'] = '15';
$config['hst_tax'] = '7';
$config['flat_shipping'] = true;
$config['flat_shipping_price'] = '15.00';
?>

The MY_Cart extension below uses a product database to extract the actual coded pricing. So you will want to update those lines to link your products to your database. The query used in the example below is a very dirty query I suggest only extracting the information you need rather than the entire products table.

Now Create another new file called MY_Cart.php and paste the following in this file. Most of the updates take place in the _save_cart() function.

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
/* The MX_Controller class is autoloaded as required */
class MY_Cart extends CI_Cart
{
 public function __construct($params = array())
 {
  // Set the super object to a local variable for use later
  $this->CI =& get_instance();

  // Are any config settings being passed manually?  If so, set them
  $config = array();
  if (count($params) > 0)
  {
   foreach ($params as $key => $val)
   {
    $config[$key] = $val;
   }
  }

  // Load the Sessions class
  $this->CI->load->library('session', $config);

  // Grab the shopping cart array from the session table, if it exists
  if ($this->CI->session->userdata('cart_contents') !== FALSE)
  {
   $this->_cart_contents = $this->CI->session->userdata('cart_contents');
  }
  else
  {
   // No cart exists so we'll set some base values
   $this->_cart_contents['cart_total'] = 0;
   $this->_cart_contents['total_items'] = 0;
   $this->_cart_contents['cart_total_discount'] = 0;
   $this->_cart_contents['cart_total_no_discount'] = 0;
  }

  log_message('debug', "Cart Class Initialized");
 }

 function _insert($items = array())
 {
  // Was any cart data passed? No? Bah...
  if ( ! is_array($items) OR count($items) == 0)
  {
   log_message('error', 'The insert method must be passed an array containing data.');
   return FALSE;
  }

  // --------------------------------------------------------------------

  // Does the $items array contain an id, quantity, price, and name?  These are required
  if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))
  {
   log_message('error', 'The cart array must contain a product ID, quantity, price, and name.');
   return FALSE;
  }

  // --------------------------------------------------------------------

  // Prep the quantity. It can only be a number.  Duh...
  $items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));
  // Trim any leading zeros
  $items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));

  // If the quantity is zero or blank there's nothing for us to do
  if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)
  {
   return FALSE;
  }

  // --------------------------------------------------------------------

  // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
  // Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
  // Note: These can be user-specified by setting the $this->product_id_rules variable.
  if ( ! preg_match("/^[".$this->product_id_rules."]+$/i", $items['id']))
  {
   log_message('error', 'Invalid product ID.  The product ID can only contain alpha-numeric characters, dashes, and underscores');
   return FALSE;
  }

  // --------------------------------------------------------------------

  // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
  // Note: These can be user-specified by setting the $this->product_name_rules variable.
  if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name']))
  {
   log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces');
   return FALSE;
  }

  // --------------------------------------------------------------------

  // Prep the price.  Remove anything that isn't a number or decimal point.
  $items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
  // Trim any leading zeros
  $items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price']));

  // Is the price a valid number?
  if ( ! is_numeric($items['price']))
  {
   log_message('error', 'An invalid price was submitted for product ID: '.$items['id']);
   return FALSE;
  }

  // --------------------------------------------------------------------

  // We now need to create a unique identifier for the item being inserted into the cart.
  // Every time something is added to the cart it is stored in the master cart array.
  // Each row in the cart array, however, must have a unique index that identifies not only
  // a particular product, but makes it possible to store identical products with different options.
  // For example, what if someone buys two identical t-shirts (same product ID), but in
  // different sizes?  The product ID (and other attributes, like the name) will be identical for
  // both sizes because it's the same shirt. The only difference will be the size.
  // Internally, we need to treat identical submissions, but with different options, as a unique product.
  // Our solution is to convert the options array to a string and MD5 it along with the product ID.
  // This becomes the unique "row ID"
  if (isset($items['options']) AND count($items['options']) > 0)
  {
   $rowid = md5($items['id'].implode('', $items['options']));
  }
  else
  {
   // No options were submitted so we simply MD5 the product ID.
   // Technically, we don't need to MD5 the ID in this case, but it makes
   // sense to standardize the format of array indexes for both conditions
   $rowid = md5($items['id']);
  }

  // --------------------------------------------------------------------

  // Now that we have our unique "row ID", we'll add our cart items to the master array

  // let's unset this first, just to make sure our index contains only the data from this submission
  unset($this->_cart_contents[$rowid]);

  // Create a new index with our new row ID
  $this->_cart_contents[$rowid]['rowid'] = $rowid;

  // And add the new items to the cart array
  foreach ($items as $key => $val)
  {
   $this->_cart_contents[$rowid][$key] = $val;
  }

  // Woot!
  return $rowid;
 }
 
 function contents()
 {
  $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']);
  unset($cart['cart_total_discount']);
  unset($cart['cart_total_no_discount']);

  return $cart;
 }
 
 function destroy()
 {
  unset($this->_cart_contents);

  $this->_cart_contents['cart_total'] = 0;
  $this->_cart_contents['total_items'] = 0;
  $this->_cart_contents['cart_total_discount'] = 0;
  $this->_cart_contents['cart_total_no_discount'] = 0;

  $this->CI->session->unset_userdata('cart_contents');
 }
 
 function total_discount()
 {
  return $this->_cart_contents['cart_total_discount'];
 }
 
 function total_no_discount()
 {
  return $this->_cart_contents['cart_total_no_discount'];
 }
 
 function _save_cart()
 {
 // Unset these so our total can be calculated correctly below
 unset($this->_cart_contents['total_items']);
 unset($this->_cart_contents['cart_total']);
 unset($this->_cart_contents['cart_total_no_discount']);
 unset($this->_cart_contents['cart_total_discount']);

 // Lets add up the individual prices and set the cart sub-total
 $total = 0;
 $total_discount = 0;
 $items = 0;
 foreach ($this->_cart_contents as $key => $val)
  {
  // We make sure the array contains the proper indexes
  if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty']))
   {
   continue;
   }

  #Lets Get the actual product stored in the database
  $this->CI->db->where('product_id', $val['id']);
  $get_product = $this->CI->db->get('products');

  #Extract the Price of the product
  foreach($get_product->result() as $gp)
   {
   $product_price = $gp->price;
   }
   
  $items += $val['qty'];
  
   #Discount Multiplier
   $discount_mult = 100 - $this->CI->config->item('discount_multi_percent');
   $discount = $discount_mult / 100;
   $price = $product_price * $discount;
   
   #Tax
   $tax_value = $this->CI->config->item('hst_tax') / 100;
   $tax_hst_discount = $price * $tax_value;
   $tax_hst = $product_price * $tax_value;
   
   if($items >= $this->CI->config->item('discount_multiple_items'))
   {
   $total += ($product_price * $val['qty'] + $tax_hst * $val['qty']);
   $total_discount += ($price * $val['qty'] + $tax_hst_discount * $val['qty']);
   $this->_cart_contents[$key]['price'] = $product_price;
   $this->_cart_contents[$key]['price_discount'] = $price;
   // Set the subtotal
   $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty'] + $tax_hst * $this->_cart_contents[$key]['qty']);
   $this->_cart_contents[$key]['subtotal_discount'] = ($this->_cart_contents[$key]['price_discount'] * $this->_cart_contents[$key]['qty'] + $tax_hst_discount * $this->_cart_contents[$key]['qty']);
   }
   else
   {   
   $total += ($product_price * $val['qty'] + $tax_hst * $val['qty']);
   $total_discount += ($price * $val['qty'] + $tax_hst_discount * $val['qty']);
   $this->_cart_contents[$key]['price'] = $product_price;
   $this->_cart_contents[$key]['price_discount'] = $price;
   // Set the subtotal
   $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty'] + $tax_hst * $this->_cart_contents[$key]['qty']);
   $this->_cart_contents[$key]['subtotal_discount'] = ($this->_cart_contents[$key]['price_discount'] * $this->_cart_contents[$key]['qty'] + $tax_hst_discount * $this->_cart_contents[$key]['qty']);
   }
  }
 
 // Set the cart total and total items.
 $this->_cart_contents['total_items'] = $items;
 $this->_cart_contents['cart_total_no_discount'] = $total;
 $this->_cart_contents['cart_total_discount'] = $total_discount;
 $this->_cart_contents['cart_total'] = $total;

 // Is our cart empty?  If so we delete it from the session
 if (count($this->_cart_contents) <= 2)
  {
  $this->CI->session->unset_userdata('cart_contents');

  // Nothing more to do... coffee time!
  return FALSE;
  }

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

 // Woot!
 return TRUE;
 }
    
}

For a view file you can use this as a starting point show_cart.php

<?
$hst_tax = $this->config->item('hst_tax');
$hst_calc = 100 - $hst_tax;
$hst_dec = '0.'.$hst_calc;
?>
<style type="text/css">
.cart{border: 2px solid #fff;
    border-collapse: collapse;
    border-spacing: 0;
    width: 100%;}
.cart th{    border: 2px solid #fff;
    color: #888;
    font-size: 1.3em;
    font-weight: bold;
    padding: 5px;}
.cart td{border: 2px solid #fff;
    color: #888;
    font-size: 1.3em;
 font-weight:bold;
    padding: 5px;}
</style>
<h6><?=$heading?></h6><br />
<?php echo form_open('paypal_gateway/updatecart'); ?>
<table class="cart">
<tr>
  <th>QTY</th>
  <th>Item Description</th>
  <th>Item Price</th>
  <th>Tax</th>
  <th>Sub-Total</th>
  <th>Sub-Total-Discount</th>
</tr>
<?php $i = 1; ?>
<?php foreach($this->cart->contents() as $items): ?>
 <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
 <tr>
   <td><fieldset><?php echo form_input(array('class' => 'field', 'name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5', 'style' => 'font-size:1.2em;')); ?></fieldset></td>
   <td>
  <?php echo $items['name']; ?>
   <?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
    <p>
     <?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
      
      <strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
          
     <?php endforeach; ?>
    </p>
    
   <?php endif; ?>
   <?php if($items['qty'] >= $this->config->item('discount_multiple_items') OR $this->cart->total_items() >= $this->config->item('discount_multiple_items')):?>
   <span><?=$this->config->item('discount_multi_percent');?>% Off!</span>
   <?php endif;?>
   </td>
   <?php
   $tax_diff = $this->cart->format_number($items['price'] * $hst_dec);
   $tax_total = $this->cart->format_number($items['price'] - $tax_diff);
   ?>
   <td><?php echo $this->cart->format_number($items['price']); ?></td>
   <td><?php echo $tax_total;?></td>
   <td>$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
   <td>$<?php echo $this->cart->format_number($items['subtotal_discount']); ?></td>
 </tr>
<?php $i++; ?>

<?php endforeach; ?>
<tr>
  <td colspan="2"><fieldset>
<input name="update" class="submit" type="submit" value="Update Cart" />
</fieldset>
<?php echo form_close();?>
<?=form_open('paypal_gateway/place_order');?>
<fieldset>
<?php if($this->cart->total_items() >= $this->config->item('discount_multiple_items')):?>
<input type="hidden" name="amount" value="<?php if ($this->cart->format_number($this->cart->total_discount()) == ''):?>0.00<?php else:?><?php echo $this->cart->format_number($this->cart->total_discount()); ?><?php endif;?>">
<?php else:?>
<input type="hidden" name="amount" value="<?php if ($this->cart->format_number($this->cart->total()) == ''):?>0.00<?php else:?><?php echo $this->cart->format_number($this->cart->total()); ?><?php endif;?>">
<?php endif;?>
<input type="submit" class="submit" name="checkout" value="Place Order" />
</fieldset>
<?=form_close();?></td>
  <td class="right"><strong>Total</strong></td>
  <td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
<tr>
<td></td>
<td></td>
  <td class="right"><strong>Discount Total (2 + Items)</strong></td>
  <td class="right">$<?php echo $this->cart->format_number($this->cart->total_discount()); ?></td>
</tr>
<?php echo $this->cart->total_items();?>
</table>
</div>

Well that's it hope you find it helpful. :)

Clone this wiki locally