Location: PHPKode > scripts > Faucart > faucart/faucart.lib.php
<?

### Very small and lightweight e-commerce cart ###
### by Fausto Iannuzzi - 2005 ###

################################################
#
# Faucart Library
#
# by Fausto Iannuzzi - 2005 - free to use
#
################################################


$cart_name		= "MYFAUCART";
$cart_object	= null;

class faucartelement {
  
  # Reserved variables
  
  var $object_id;
  var $name;
  var $description;
  var $price;
  var $quantity;

  function faucartelement($name,$description,$price) {
	  $this->name = $name;
  	  $this->description = $description;
  	  $this->price = $price;
  	  $this->quantity = 1;
	  $this->object_id = md5($name.$description.$price);
  }
}

class faucart {

 # Reserved variables
 var $_items;
 var $_current_item;
 var $_total;

 function faucart() {
	 $this->_items = array();
	 $this->_current_item = 0;
	 $this->_total = 0;
 }

 function isFound($item_number) {
  for ($i=0;$i<count($this->_items);$i++) {
	  if (isset($this->_items[$i])) {
			 if ($this->_items[$i]->object_id == $item_number->object_id)
				 return $i;
		 }
	 }
	 return false;
 }

function atIndex($object_id) {
  for ($i=0;$i<count($this->_items);$i++) {
	  if (isset($this->_items[$i])) {
			 if ($this->_items[$i]->object_id == $object_id)
				 return $i;
		 }
	 }
	 return false;
 }

 function addItem($item) {
	$index = $this->isFound($item);
	if ($index!==false)
		; #$this->updateQuantity($item,1,$index);
	else {
		$this->_items[] = $item;
	}
 }

 function delItem($item_number) {
  $temp_items = array();
  for ($i=0;$i<count($this->_items);$i++) {
  if (isset($this->_items[$i])) {
   if ($this->_items[$i]->object_id == $item_number->object_id)
				 continue;
   else
	$temp_items[] = $this->_items[$i];
   }
  }
  $this->_items = $temp_items;
  unset($temp_items);
 }

 function delItemByObjectID($object_id) {
  $temp_items = array();
  for ($i=0;$i<count($this->_items);$i++) {
  if (isset($this->_items[$i])) {
   if ($this->_items[$i]->object_id == $object_id)
				 continue;
   else
	$temp_items[] = $this->_items[$i];
   }
  }
  $this->_items = $temp_items;
  unset($temp_items);
 }


 function getItems() {
	 return $this->_items;
 }

 function updateQuantity($item,$delta,$index=false) {
	 if ($index == false) {
	  $index = $this->isFound($item);
	 }
     $this->_items[$index]->quantity += $delta; 
 }

 function emptyCart() {
	 unset($this->_items);
	 $this->_items = array();
	 $this->_total = 0;
 }

 function total() {
  $this->_total = 0;
  for ($i=0;$i<count($this->_items);$i++) {
		 if (isset($this->_items[$i])) {
			 $this->_total += number_format((double)($this->_items[$i]->quantity*$this->_items[$i]->price),2, '.', '');
		 }
	 }
 }

}

function inSession() {
	global $cart_name;
	global $cart_object;

	if (isset($_SESSION[$cart_name])) 
		$cart_object = unserialize($_SESSION[$cart_name]);
	else 
		$cart_object = new faucart();
}

function toSession() {
	global $cart_name;
	global $cart_object;

	if (isset($_SESSION[$cart_name])) 
		unset($_SESSION[$cart_name]);
	$_SESSION[$cart_name] = serialize($cart_object);
}

inSession();
?>
Return current item: Faucart