Shopping Cart Tutorial – how to build a php shopping cart website

May 31, 2012, by admin

shopping cart tutorialThis is yet another shopping cart tutorial. But the technique i will discuss is quick and easy to understand, all you need to learn this tutorial is the knowledge of PHP sessions and some array functions. So i am not going to tell you what a shopping cart is? What are sessions and how they work, i will rather jump to how we are going to build a shopping cart.

How to Build a Shopping Cart using php

First of all i would like to discuss the database structure for this shopping cart. The shopping cart will have 4 tables, although the shopping process needs only 1 table i.e products, the other three tables (customers, orders and order_detail ) will be needed to store successful orders. The structure of all these tables is shown below

relationship_diagramDatabase Tables

Products              The products in this table will be shown to the user, she will add these products to her shopping cart

Customers          When the user places order, her billing detail will be stored in this table

Orders  There will be a single entry for each successful order. Each order will be assigned a unique serial/order number

Order Detail       There will be a single entry for each item purchased. Although price is available from the products table, but if the price of an item changes in the products table, it should not be changed in the order_detail table. So these are two different fields.

Lets now come to PHP. The shopping process will work like this:

The user includes a product to his/her shopping cart

The PHP script (products.php) receives this action and stores the requested product in the session array

The PHP script redirects to a different page (shoppingcart.php) which displays purchased items to the user.

How the products will be stored in the session array ?

The cart will be a 2 dimensional session array to store productid and quantity of every item purchased. The table below shows 3 items in the shopping cart.

Index    productid            qty

0               235                     1

1             239                        2

2              287                      1

And the code to store these 3 items in the session array, is given below

$max=0;

$_SESSION[‘cart’][$max][‘productid’]=235;

$_SESSION[‘cart’][$max][‘qty’]=1;

 

$max++;

$_SESSION[‘cart’][$max][‘productid’]=239;

$_SESSION[‘cart’][$max][‘qty’]=2;

 

$max++;

$_SESSION[‘cart’][$max][‘productid’]=287;

$_SESSION[‘cart’][$max][‘qty’]=1;

 Important Shopping Cart Functions

The following utility functions are involved in the process of shopping

Addtocart

function addtocart($pid,$q){

if($pid<1 or $q<1) return;

 

if(is_array($_SESSION[‘cart’])){

if(product_exists($pid)) return;

$max=count($_SESSION[‘cart’]);

$_SESSION[‘cart’][$max][‘productid’]=$pid;

$_SESSION[‘cart’][$max][‘qty’]=$q;

}

else{

$_SESSION[‘cart’]=array();

$_SESSION[‘cart’][0][‘productid’]=$pid;

$_SESSION[‘cart’][0][‘qty’]=$q;

}

}

The addtocart function makes sure that the session variable is initialzed and then stores the received productid and quantiy to next available index. And note that there is no need to increment the $max variable

product_exists

function product_exists($pid){

$pid=intval($pid);

$max=count($_SESSION[‘cart’]);

$flag=0;

for($i=0;$i<$max;$i++){

if($pid==$_SESSION[‘cart’][$i][‘productid’]){

$flag=1;

break;

}

}

return $flag;

}

The function goes through all the elements of shopping cart and checks if a products exists in the shopping cart

Remove_product

function remove_product($pid){

$pid=intval($pid);

$max=count($_SESSION[‘cart’]);

for($i=0;$i<$max;$i++){

if($pid==$_SESSION[‘cart’][$i][‘productid’]){

unset($_SESSION[‘cart’][$i]);

break;

}

}

$_SESSION[‘cart’]=array_values($_SESSION[‘cart’]);

}

The remove_product function first finds the product and then removes the corresponding index from the session array. The last statement { $_SESSION[‘cart’]=array_values($_SESSION[‘cart’]) } resets the array indexes.

About PHP files in this tutorial

 There are only 3 php files in the demo (in addition to functions.php and db.php file)

products.php (displays products to the user)

shoppingcart.php (displays items in the shopping cart to the user)

billing.php (updates customer billing information in the database)

Thats all in this tutorial.