get_contents();
The functions are documented below.
ADDING AN ITEM
$cart->add_item( code , quantity , price , info )
If you have a widget with code AB313 and a price of $40.11,
you can add it to the cart with :
$cart->add_item('AB313',1,40.11,'Widget AB313 Standard');
if the item AB313 is alreay in the cart, then the quantity of it
will be increased by the quantity supplied to the function, in
this case 1
the itemid must be unique, and can be letters and/or numbers.
DELETING AN ITEM
$cart->del_item( itemid );
CHANGING A QUANTITY
$cart->edit_item( itemid, new_quantity );
if you set the quanitity to zero, the item is deleted.
GETTING THE NUMBER OF ITEMS IN CART
echo $cart->itemcount;
GETTING THE TOTAL VALUE OF THE CART
echo $cart->total;
EMPTING THE CART
$cart->empty_cart();
GETTING THE CART CONTENTS
$items = $cart->get_contents();
.. and how to display them ..
foreach($items as $item) {
echo "Code/ID :".$item['id']." ";
echo "Quantity:".$item['qty']." ";
echo "Price :".$item['price']." ";
echo "Info :".$item['info']." ";
echo "Subtotal".$item['subtotal']." ";
}
note, subtotal is quantity X price for that item, not a running total..
EXTENDING Webforce Cart
There are 2 functions you can create that will enable more flexability.
One is wf_get_price( itemid, qty ). If you need to do pricing based on Quantity
( e.g. bulk discounts ) then you can create a function called wf_get_price to do what you need itto do. If you do not pass a price to add_item, then the wf_
get_price function will be called. If you use this functionality,
AND your price is quantity dependant, then you must uncomment the line in
the edit_item function.
The other is wf_get_info, again, if you do not pass info to add_item
then get_info will be called.
TIP: you do not have to pass a string to add_item(), you could use an array of
infomation instead. e.g.
add_item( 'AS2112',5,19.95,array('size'=>'large','color'=>'red'));
then when you use get_contents, you echo $item['info']['color'] etc.
BUGS/PATCHES
Please email eaden@webforce.co.nz with any bugs/fixes/patches/comments etc.
See http://www.webforce.co.nz/cart/ for updates to this script
*/
class wfCart {
var $total = 0;
var $itemcount = 0;
var $items = array();
var $itemprices = array();
var $itemqtys = array();
var $iteminfo = array();
function cart() {} // constructor function
function get_contents()
{ // gets cart contents
$items = array();
foreach($this->items as $tmp_item)
{
$item = FALSE;
$item['id'] = $tmp_item;
$item['qty'] = $this->itemqtys[$tmp_item];
$item['price'] = $this->itemprices[$tmp_item];
$item['info'] = $this->iteminfo[$tmp_item];
$item['subtotal'] = $item['qty'] * $item['price'];
$items[] = $item;
}
return $items;
} // end of get_contents
function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE)
{ // adds an item to cart
if(!$price)
{
$price = wf_get_price($itemid,$qty);
}
if(!$info)
{
$info = wf_get_info($itemid);
}
if($this->itemqtys[$itemid] > 0)
{ // the item is already in the cart..
// so we'll just increase the quantity
$this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid];
$this->_update_total();
} else {
$this->items[]=$itemid;
$this->itemqtys[$itemid] = $qty;
$this->itemprices[$itemid] = $price;
$this->iteminfo[$itemid] = $info;
}
$this->_update_total();
} // end of add_item
function edit_item($itemid,$qty)
{ // changes an items quantity
if($qty < 1) {
$this->del_item($itemid);
} else {
$this->itemqtys[$itemid] = $qty;
// uncomment this line if using
// the wf_get_price function
// $this->itemprices[$itemid] = wf_get_price($itemid,$qty);
}
$this->_update_total();
} // end of edit_item
function del_item($itemid)
{ // removes an item from cart
$ti = array();
$this->itemqtys[$itemid] = 0;
foreach($this->items as $item)
{
if($item != $itemid)
{
$ti[] = $item;
}
}
$this->items = $ti;
$this->_update_total();
} //end of del_item
function empty_cart()
{ // empties / resets the cart
$this->total = 0;
$this->itemcount = 0;
$this->items = array();
$this->itemprices = array();
$this->itemqtys = array();
$this->itemdescs = array();
} // end of empty cart
function _update_total()
{ // internal function to update the total in the cart
$this->itemcount = 0;
$this->total = 0;
if(sizeof($this->items > 0))
{
foreach($this->items as $item) {
$this->total = $this->total + ($this->itemprices[$item] * $this->itemqtys[$item]);
$this->itemcount++;
}
}
} // end of update_total
}
?>