[eluser]bgreene[/eluser]
our site has products at various tax rates eg food, stationery etc so my approach was to use the following code when inserting an item (pardon the mess, i'm no expert but if might help you along the way.
Code:
function _packadd($pack,$qty){
$rowid = false;
if (! $qty) $qty = 1;
else
if ($qty <= 0) $qty = 1;
$data = array(
'id' => $pack->id,
'price' => $pack->rate,
'name' => $pack->code, // eg CF214
'qty' => $qty,
'options' => array(
'title' => $pack->caption, // eg cat food, very tasty
'vatcodeid' => $pack->vatcode_id, // eg 316
'vatcode' => $pack->vatcode, // eg S,
'vatrate' => $pack->vatrate, // eg 21.5
'weight' => $pack->weight,
'thumb' => $pack->thumb, // thumbnail for the trolley
'prodid' => $pack->prod_id
)
);
foreach ($this->cart->contents() as $items){
if ($items['id'] == $data['id']) {
$rowid = $items['rowid'];
$this->cart->update(
array('rowid'=>$items['rowid'], 'qty' => $items['qty']+$data['qty'] )
);
break;
}
}
if (! $rowid) {$rowid = $this->cart->insert($data);}
return $rowid;
}
and then, i created a MY_Cart as follows
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Cart extends CI_Cart {
public $showreverseorder = true;
public function __construct() {
parent::__construct();
}
function vatinclusive(){
return $this->CI->session->userdata('showvatinc');
}
public function deleteall(){
$ar = array();
foreach ($this->contents() as $item){
$ar[] = array('rowid'=>$item['rowid'],'qty'=> 0);
}
$this->update($ar);
}
public function trolleySummary(){
if ($this->total_items() == 0)
return '<a href="'. site_url('trolley') .'">Trolley<br/>Products: '.'0'.
'<br/>Total: € '.money_str(0) .'<br/><span>View Contents</span></a>';
else
if ($this->vatinclusive())
return '<a href="'. site_url('trolley') .'">Trolley<br/>Products: '.$this->total_items().
'<br/>Total: € '
.money_str($this->nett()+$this->nettVat() ).'<br/><span>View Contents</span></a>';
else
return '<a href="'. site_url('trolley') .'">Trolley<br/>Products: '.$this->total_items().
'<br/>Total: € '.money_str($this->nett()).'<br/><span>View Contents</span></a>';
}
public function trolleyTable($id,$readonly=false){
$s = '
<table class="seltbl" id="'.$id.'">
<thead> <tr> <th>Code</th> <th>Description</th>
<th>Image</th>
<th>Qnty</th>
<th>Rate</th>
<th>VAT</th>
<th>Amount</th>
</tr>
</thead><tbody id="trolleyItems">';
$b = false;
$ar = $this->contents();
if ($this->showreverseorder) $ar = array_reverse($ar);
foreach ($ar as $items){
if ($b) $sx .= 'class="altrow"';
else $sx = '';
$b = !$b;
$s .= '<tr
<td class="pack_code" >'.$items['name'].'<br/><span>(details...)</span></td>
<td class="pack_title">'.$items['options']['title'].'</td>
<td><img src="'. static_url('images/products/small/'.$items['options']['thumb']) .'"
class="prod_thumbnail" alt="thumbnail" /></td>
<td class="pack_qnty">';
if ($readonly) $s .= $items['qty'];
else
$s .= '<input class="pack_qntyedit" id="'.$items['rowid'].'" name="Qty" value="'.$items['qty'].'" maxlength="4"/ >';
$s .='</td>
<td class="pack_rate">'.money_str($items['price']).'</td><td class="pack_vat">'.$items['options']['vatcode'].'</td>
<td class="pack_amt">'.money_str($items['subtotal']).'</td></tr>';
}
if ($this->total_items() > 0)
$s .= '<tr><td class="pack_code">'.'DLVRY'.'</td>
<td class="pack_title">'.'Delivery Charge'.'</td>
<td> </td>
<td class="pack_qnty">1</td>
<td class="pack_rate">'.money_str($this->freight()).'</td><td class="pack_vat">'.$this->CI->config->item('freight_vat_code').'</td>
<td class="pack_amt">'.money_str($this->freight()).'</td></tr>';
$s .= '
<tr>
<td colspan="5"></td>
<td class="trolley_totalname">Nett</td>
<td class="trolley_totalamt">€ '.money_str($this->nett()+$this->freight()) .'</td>
</tr>
<tr>
<td colspan="5"></td>
<td class="trolley_totalname">VAT</td>
<td class="trolley_totalamt">€ '.money_str($this->nettVat()+$this->freightVat()) .'</td>
</tr>
<tr>
<td colspan="5"></td>
<td class="trolley_totalname">Total</td>
<td class="trolley_totalamt">€ '.money_str($this->gross()) .'</td>
</tr>
</tbody>
</table>';
return $s;
}
public function freight(){
if ($this->total_items() == 0) return 0;
else
if (($this->nett()+$this->nettVat()) >= $this->CI->config->item('freight_freeabove')) return 0;
else return $this->CI->config->item('freight_dfltrate');
}
public function freightVat(){
return $this->freight()*$this->CI->config->item('freight_vat_rate')/100;
}
public function gross(){
return $this->nett()+$this->nettVat()+$this->freight()+$this->freightVat();
}
public function nett(){
return $this->total();
}
public function nettVat(){
$vatrates = array();
foreach ($this->contents() as $item){
$vr = $item['options']['vatrate'];
if (! array_key_exists($vr,$vatrates)) $vatrates[$vr] = 0;
$vatrates[$vr] += ($item['price']*$item['qty']);
}
$result = 0;
foreach ($vatrates as $vr=>$nett) $result += $nett*$vr/100;
return $result;
}
}