Location: PHPKode > scripts > Shopping Cart > shopping-cart/class.cart.php
<?
/*############################################################
A smarty based manageable shopping cart class 

Authors: Satya Prakash 
        Deepak Singh Tanwar.
if you have any queries please mail me at hide@address.com
############################################################
*/
	class Cart {

		public $products,$cart_content, $expiretime, $table;

	function __construct()
	{
		$this->table ="tblbed_shopping";
		$this->user ="tblbed_customer";
		$this->expiretime = time()+24*3600*5;

	}

		function check_item($db, $session, $product) {
			$query = "SELECT * FROM $this->table WHERE sessionid='$session' AND product_id='$product' ";
			$result = $db->select($query);
			if(!$result) {
				return 0;
			}

			$numRows = $db->numrows($query);

			if($numRows == 0) {
				return 0;
			} else {
				//$row = mysql_fetch_object($result);
				return $result[0]['quantity'];
			}
		}

		function add_item($db, $session, $product, $quantity=1) {

			$qty = $this->check_item($db, $session, $product);
			if($qty == 0) {
				$record['sessionid']=$db->mySQLSafe($session);
				$record['product_id']=$db->mySQLSafe($product);
				$record['quantity']=$db->mySQLSafe($quantity);

				$db->insert($this->table,$record);

			} else {
				$quantity = $qty+$quantity;
				$record['sessionid']=$db->mySQLSafe($session);
				$record['product_id']=$db->mySQLSafe($product);
				$record['quantity']=$db->mySQLSafe($quantity);
				$where ="sessionid='$session' AND product_id='$product' ";

				$db->update ($this->table, $record, $where);
			}
		}

		function delete_item($db, $session, $product) {
			$where = "sessionid='$session' AND product_id='$product' ";
			//$query = "DELETE FROM $table WHERE session='$session' AND product='$product' ";
			//mysql_query($query);
			$db->delete($this->table,$where);
		}

		function modify_quantity($db, $session, $product, $quantity) {
			$record['quantity']=$db->mySQLSafe($quantity);
			$where = "sessionid='$session' AND product_id='$product' ";
			//$query = "UPDATE $this->table SET quantity='$quantity' WHERE session='$session' ";
			//$query .= "AND product='$product' ";
			$db->update($this->table,$record,$where);
			//mysql_query($query);
		}

		function clear_cart($db, $session) {
			$where = "sessionid='$session' ";

			$db->delete($this->table,$where);
		}

		function cart_total($db, $session) {
			$query = "SELECT * FROM $this->table WHERE sessionid='$session' ORDER BY id ";
			$result = $db->select($query);
			$catnumrows = $db->numrows($query);
			for ($i=0; $i<$catnumrows; $i++){
			  $pquery = "select product_id,pcode,supplier,model,size,retailIncVat,tblbed_inventory.cat_id,cat_name from tblbed_inventory,tblbed_category where tblbed_inventory.cat_id= tblbed_category.cat_id and product_id ='".$result[$i]["product_id"]."'";

					$total += smartPrice($result[$i]["quantity"]*$prodresult[0]['retailIncVat']);
				}

			return $total;
		}

		function display_contents($db, $session) {
			$count = 0;
			$k=0;
			//echo $session ;
			$query = "SELECT * FROM $this->table WHERE sessionid='$session' ORDER BY id ";
			$result = $db->select($query);
			$catnumrows = $db->numrows($query);

			for ($i=0; $i<$catnumrows; $i++){
				//echo $result[$j]["product_id"];
				$pquery = "select tblbed_inventory.product_id,img,pcode,( SELECT optionvalue FROM tblbed_option WHERE id = supplier ) AS supplier,model,( SELECT optionvalue FROM tblbed_option WHERE id = size ) AS size,retailIncVat,tblbed_inventory.cat_id,cat_name from tblbed_category,tblbed_inventory left join tblbed_img_idx on tblbed_inventory.product_id = tblbed_img_idx.product_id where tblbed_inventory.cat_id= tblbed_category.cat_id and tblbed_inventory.product_id ='".$result[$i]["product_id"]."'";
				$prodresult = $db->select($pquery);
				$this->products[$i] = $prodresult[0];
				$this->products[$i]['pricePItem'] = smartPrice($prodresult[0]['retailIncVat']);
				$this->products[$i]['cost'] = smartPrice($result[$i]["quantity"]*$prodresult[0]['retailIncVat']);
				$this->products[$i]['quantity'] = $result[$i]["quantity"];
				$this->products[$i]['smartSize'] = get_feetinch($this->products[$i]['size'],$db,$this->products[$i]['cat_id']);
				$k += $result[$i]["quantity"]*$prodresult[0]['retailIncVat'];
				$this->cart_content['product']=$this->products;
			}
			$this->cart_content['total']=$k;
			//print_r($this->cart_content);
		//return $this->cart_content;
		}

		function num_items($db, $session) {
			$query = "SELECT * FROM $this->table WHERE sessionid='$session' ";
			//$result = $db->select($query);
			$num_rows =$db->numrows($query);
			return $num_rows;
		}

		function quant_items($db, $session) {
			$quant = 0;
			$query = "SELECT * FROM $this->table WHERE sessionid='$session' ";
			$result = $db->select($query);
			$num_rows =$db->numrows($query);
			for($i=0;$i<numrows;$i++) {
				$quant += $result[$i]['quantity'];
			}
			return $quant;
		}


		function userinfo($db) {
			$quant = 0;
			$_SESSION['customer_id'] = 34;

			$query = "SELECT firstname,lastname,email,add_1,add_2,town,county,postcode,phone FROM ".$this->user." WHERE customer_id=".$_SESSION['customer_id'];

			$result = $db->select($query);
			$num_rows =$db->numrows($query);

			return $result;
		}

		function checkout($session,$request)
		{
			global $db;
			if(!empty($session['ccUserfirstName']))
			{
				header("Location: index.php?action=payment");
				exit;
			}else{
				if(!empty($request['login_x'])) {
					$loginQuery = "select * from tblbed_customer where email='".$request['umail']."' and password ='".$request['upass']."'";
					$loginRes = $db->numrows($loginQuery);

					if($loginRes > 0) {
						$uFirstName = $db->select($loginQuery);
						$_SESSION['ccUserfirstName'] = $uFirstName[0]['firstName'];
						$_SESSION['customer_id'] = $uFirstName[0]['customer_id'];
						if(isset($request['rememberMe'])){
							setcookie("umail",$request['umail'],$this->expiretime);
							setcookie("upass",$request['upass'],$this->expiretime);
						}
						header("Location: index.php?action=checkout");
						exit;
					}else{
						$msg ="Invalid Login";
					}
				}
			}
		}

	}


?>
Return current item: Shopping Cart