Creating E-commerce cart system in PHP

php-shopping-cart

Hi, friend today i am writing tutorial on cart system. Well ,if you are a programmer or just fresher ,taking training somewhere, you know that making e-commerce/shopping cart site (cart system ) is so famous. Maximum company required that things.

For better explanation i have divided major sections to.

section 1: Creating products display category wise

For this we have to create 2 table in our MySQL database

– category (cid,name)

– products(pid,title,body,image,category,color,model,cost)

you can find below sql query to create ‘category’ table

CREATE TABLE IF NOT EXISTS `category` (
  `cid` int(255) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`cid`)
);

SQL query to create ‘products’ table

CREATE TABLE IF NOT EXISTS `products` (
  `pid` int(255) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `body` text NOT NULL,
  `image` varchar(255) NOT NULL,
  `cid` int(255) NOT NULL,
  `col_id` int(255) NOT NULL,
  `model` varchar(40) NOT NULL,
  `cost` mediumint(40) NOT NULL,
  PRIMARY KEY (`pid`)
)

 

Section: 2 Showing Products in frontend

Previously we have created category and products table ,then added some data in it.Now time to show these in our site.
let’s create our php project .For this we need to have any HTML template .We need to setup our HTML template to PHP.Now inside ‘includes’ folder we have left.php file .Open it in any php editor and put following code where you want to display your categories.

<div class="title_box">Categories</div>
Similarly we have to create a page i.e 'products.php' and put following code
<div class="prod_box">
<div class="top_prod_box"></div>
<div class="center_prod_box">
<div class="product_title"></div>
<div class="product_img"><a class="example-image-link" href="uploads/&lt;?php echo $row['image']; ?&gt;" data-lightbox="example-set" data-title="Click "><img class="example-image" src="uploads/&lt;?php echo $row['image']; ?&gt;" alt="" width="79%" /></a></div>
<div class="prod_price"><span class="price"> Rs. <?php echo $row['cost']; ?> </span></div>
</div>
<div class="bottom_prod_box"></div>
<div class="prod_details_tab"><a href="&quot;products.php?action=add_to_cart&amp;pid=<?php">" > <img class="left_bt" src="images/cart.gif" alt="" border="0" /> </a> <a title="header=[Specials] body=[ ] fade=[on]" href="http://www.free-css.com/"><img class="left_bt" src="images/favs.gif" alt="" border="0" /></a> <a title="header=[Gifts] body=[ ] fade=[on]" href="http://www.free-css.com/"><img class="left_bt" src="images/favorites.gif" alt="" border="0" /></a> <a class="prod_details" href="product_details.php?pid=<?php echo $row['pid']; ?>">details</a></div>
</div>

Now if we click on left category ,there should open product of the category.

Section: 3 Cart system

managing cart system is not so complicated.
Just have to create a ‘cart’ table and do some mysql query to add data into this .
To create cart table execute following sql code in phpmyadmin


CREATE TABLE IF NOT EXISTS `cart` (

  `cart_id` int(255) NOT NULL AUTO_INCREMENT,
  `pid` int(255) NOT NULL,
  `qty` int(255) NOT NULL,
  `mid` int(255) NOT NULL,
  PRIMARY KEY (`cart_id`)
)

Creating add to cart system in our products.php. Write below code in this page.

 
 $action=$_GET['action'];

	   if($action=='add_to_cart'){
	   		$pid=$_GET['pid'];
			$mid=$_SESSION['mid'];

	   	$cart->add_to_cart($pid,$mid);

	   }

I have created add to cart in oops .So find below the class ‘cart’.This code can put inside ‘class.php’ inside ‘includes’ folder .Then call it in your template ‘header.php’ file .Do not forget to create object of the class to use it’s method.

class cart{

	function add_to_cart($pid,$mid){
		$q="INSERT INTO cart (pid,qty,mid) VALUES('$pid','1','$mid') ";
		$r=mysql_query($q) or die(mysql_error());
		header("Location:cart.php");

	}

	function remove_cart($cart_id){
		$q="DELETE FROM cart WHERE cart_id='$cart_id'";
		$r=mysql_query($q) or die(mysql_error());
		header("Location:cart.php");

	}

}

So now if we click on ‘add to cart’ button in products.php page ,the product will be added to cart table along with member mid.

Section: 4 Cart.php to show cart data

Finally we are in cart.php file .Here we have to show all our cart data. Use bphp cart pageelow code to show cart products details.

<form action="" method="post">
<table border="0" width="100%">
<tbody>
<tr>
<td width="158">Image</td>
<td width="158">Title</td>
<td width="134">Qty</td>
<td width="127">Sub Total</td>
<td width="138">Remove</td>
</tr>
<?php $q="SELECT * FROM cart c INNER JOIN products p ON c.pid=p.pid WHERE mid='$mid'"; $r=mysql_query($q); $net_total=0; while($row=mysql_fetch_array($r)){ ?>
<tr>
<td><img src="uploads/&lt;?php echo $row['image']; ?&gt;" alt="" width="80" /></td>
<td></td>
<td><input style="width: 30px;" name="qty[&lt;?php echo $row['cart_id']; ?&gt;]" type="text" value="&lt;?php echo $row['qty']; ?&gt;" /></td>
<td></td>
<td><a href="cart.php?action=remove_cart&amp;cart_id=&lt;?php echo $row['cart_id']; ?&gt;">X</a></td>
</tr>
<?php } ?>
<tr>
<td></td>
<td></td>
<td><input name="update" type="submit" value="update" /></td>
<td>Net total
<?php echo $net_total; ?></td>
<td></td>
</tr>
</tbody>
</table>
</form> 

 

So, Finally we have build a cart system . For complete e-commerce site ,there lots of section still required like delivery information page, order review page ,payment gateway implementation etc.I will write tutorial for these soon .Hope this tutorial will help any  beginners for understanding core PHP shopping cart system with MySQL database.For complete E-commerce project based Web Development Training visit our site

  • Pingback: Paypal integration in code PHP Project |()

  • Ruchi Kumari

    hi ineed a code for add to cart and for payment method

    • Vara Prasad

      Hey, i tried the same code in php and using for my Online Shopping Website…

Online Web Development Training,Video Tutorials