A Shopping Cart using PHP Sessions

This posts illustrates a relatively simple shopping cart using PHP sessions to store the cart details, quantities and total cart amount. The cart has an ‘add to cart’, ‘remove from cart’ and displays the total value of the cart.

This post is taken from part of the book PHP Tutorials: Programming with PHP and MySQL which is available as a paper back printed version or as a downloadable Kindle version. Click here for paper back version.

The following listing is the complete cart:

<?php session_start();
#cart.php - A simple shopping cart with add, and remove links

//Define the products and cost
$products = array("product A", "product B", "product C");
$amounts = array("19.99", "10.99", "2.99");

 //---------------------------
 //initialise sessions
 if ( !isset($_SESSION["total"]) ) {
   $_SESSION["total"] = 0;
   for ($i=0; $i< count($products); $i++) {
    $_SESSION["qty"][$i] = 0;
   $_SESSION["amounts"][$i] = 0;
  }
 }

 //---------------------------
 //Reset
 if ( isset($_GET['reset']) )
 {
 if ($_GET["reset"] == 'true')
   {
   unset($_SESSION["qty"]);     //The quantity for each product
   unset($_SESSION["amounts"]); //The amount from each product
   unset($_SESSION["total"]);   //The total cost
   unset($_SESSION["cart"]);    //Which item has been chosen
   }
 }

 //---------------------------
 //Add
 if ( isset($_GET["add"]) )
   {
   $i = $_GET["add"];
   $qty = $_SESSION["qty"][$i] + 1;
   $_SESSION["amounts"][$i] = $amounts[$i] * $qty;
   $_SESSION["cart"][$i] = $i;
   $_SESSION["qty"][$i] = $qty;
 }

  //---------------------------
  //Delete
  if ( isset($_GET["delete"]) )
   {
   $i = $_GET["delete"];
   $qty = $_SESSION["qty"][$i];
   $qty--;
   $_SESSION["qty"][$i] = $qty;
   //remove item if quantity is zero
   if ($qty == 0) {
    $_SESSION["amounts"][$i] = 0;
    unset($_SESSION["cart"][$i]);
  }
 else
  {
   $_SESSION["amounts"][$i] = $amounts[$i] * $qty;
  }
 }
 ?>
 <h2>List of All Products</h2>
 <table>
   <tr>
   <th>Product</th>
   <th width="10px">&nbsp;</th>
   <th>Amount</th>
   <th width="10px">&nbsp;</th>
   <th>Action</th>
   </tr>
 <?php
 for ($i=0; $i< count($products); $i++) {
   ?>
   <tr>
     <td><?php echo($products[$i]); ?></td>
     <td width="10px">&nbsp;</td>
     <td><?php echo($amounts[$i]); ?></td>
     <td width="10px">&nbsp;</td>
     <td><a href="?add=<?php echo($i); ?>">Add to cart</a></td>
   </tr>
   <?php
 }
 ?>
   <tr>
   <td colspan="5"></td>
   </tr>
   <tr>
   <td colspan="5"><a href="?reset=true">Reset Cart</a></td>
   </tr>
   </table>
 <?php
 if ( isset($_SESSION["cart"]) ) {
 ?>
   <br/><br/><br/>
   <h2>Cart</h2>
   <table>
     <tr>
     <th>Product</th>
     <th width="10px">&nbsp;</th>
     <th>Qty</th>
     <th width="10px">&nbsp;</th>
     <th>Amount</th>
     <th width="10px">&nbsp;</th>
     <th>Action</th>
     </tr>
 <?php
 $total = 0;
 foreach ( $_SESSION["cart"] as $i ) {
 ?>
   <tr>
     <td><?php echo( $products[$_SESSION["cart"][$i]] ); ?></td>
     <td width="10px">&nbsp;</td>
     <td><?php echo( $_SESSION["qty"][$i] ); ?></td>
     <td width="10px">&nbsp;</td>
     <td><?php echo( $_SESSION["amounts"][$i] ); ?></td>
     <td width="10px">&nbsp;</td>
     <td><a href="?delete=<?php echo($i); ?>">Delete from cart</a></td>
 </tr>
 <?php
  $total = $total + $_SESSION["amounts"][$i];
  }
  $_SESSION["total"] = $total;
  ?>
   <tr>
   <td colspan="7">Total : <?php echo($total); ?></td>
   </tr>
   </table>
   <?php
 }
 ?>

The cart example uses the following sessions to maintain the state of the cart:

$_SESSION[“qty”][i] Stores the quantity for each product
$_SESSION[“amounts”][i] Stores the price from each product
$_SESSION[“cart”][i] Identifies which items have been added to the cart
$_SESSION[“total”] Stores the total cost

The sessions are actually arrays so in the case of:

$_SESSION[“qty”][i]

is the quantity for the element with number i.

Description of the shopping cart code

We start by defining PHP to use sessions by:

session_start();

This has to be at the very top of the PHP page.

Next we set up our products and populate our sessions. In this example we are using a fixed array of product descriptions and amounts. You may want to do this in your application or you could read in the data into the $product and $amounts array from a database.

//---------------------------
//initialise sessions

//Define the products and cost
$products = array("product A", "product B", "product C");
$amounts = array("19.99", "10.99", "2.99");

 if ( !isset($_SESSION["total"]) ) { 

  $_SESSION["total"] = 0;

  for ($i=0; $i< count($products); $i++) {
   $_SESSION["qty"][$i] = 0;
   $_SESSION["amounts"][$i] = 0;
 }
}

The following code is used to reset the sessions and clear the cart when the rest cart link is pressed.

//---------------------------
  //Reset
  if ( isset($_GET['reset']) )
   {
    if ($_GET["reset"] == 'true')
    {
     unset($_SESSION["qty"]); //The quantity for each product
     unset($_SESSION["amounts"]); //The amount from each product
     unset($_SESSION["total"]); //The total cost
     unset($_SESSION["cart"]); //Which item has been chosen
   }
}

The following code adds an item to the sessions when the ‘Add to Cart’ link is clicked:

//---------------------------
//Add
if ( isset($_GET["add"]) )
{
$i = $_GET["add"];

$qty = $_SESSION["qty"][$i] + 1;

$_SESSION["amounts"][$i] = $amounts[$i] * $qty;
$_SESSION["cart"][$i] = $i;
$_SESSION["qty"][$i] = $qty;
}

and the following deletes an item from the cart when the ‘Delete from Cart’ link is clicked:

 //---------------------------
 //Delete
 if ( isset($_GET["delete"]) )
 {
   $i = $_GET["delete"];
   $qty = $_SESSION["qty"][$i];
   $qty--;
   $_SESSION["qty"][$i] = $qty;

 //remove item if quantity is zero
 if ($qty == 0) {
   $_SESSION["amounts"][$i] = 0;
   unset($_SESSION["cart"][$i]);
 }
 else
 {
   $_SESSION["amounts"][$i] = $amounts[$i] * $qty;
 }
 }

The rest of the code is the visual display using a table and various loops to show the product lists and the cart details together with the links.

This post is taken from part of the book PHP Tutorials: Programming with PHP and MySQL which is available as a paper back printed version or as a downloadable Kindle version. Click here for paper back version.