Saturday, February 20, 2010

Session Based Shopping Cart

Here is a session based cart I used for a site where it was not beneficial to have a database driven cart. This client used a merchant service for credit card processing but the basic account only provided the check out process...



All I needed to do was pass the cart contents and the client can print the invoice and store the order details at the merchant's site. There is a data-driven portion holding the inventory. Hope this is helpful for someone else. For more information look into using multi-dimensional arrays.

This tutorial assumes you have an understanding of PHP and some basic functions. This is the section of the code that manages the cart itself. We created the variable at the start of the session to hold the cart. The second variable interprets the action we want to perform.

$cart = $_SESSION['cart'];
$action = $_GET['action'];


The next part of the code handles all the cart actions. It also handles the case that someone clicked on a product already in the cart, in this case it will simply add 1 to the quantity. Also, we are getting the item id passed to us in the url or via a form post.

switch ($action) {
case 'clear':
unset($_SESSION['cart']);
unset($cart);
break;
case 'add':
$newitem = $_GET['item'];
//check to see if I already have this key
if(array_key_exists($newitem, $cart)){
++$cart[$newitem];
}
else {
$cart[$newitem] = 1;
}
break;

case 'delete':
unset($cart[$_GET['item']]);
break;

case 'update':
if ($cart) {
foreach ($_GET as $key=>$value) {
if (stristr($key,'qty')) { //this is our qty and id
$id = str_replace('qty','',$key);
$qty = $_GET[$key];
$cart[$id] = $qty;
}
} // end foreach
} // end if
break;

} //end switch


The cart I implemented is a bit more advanced than this but I tried to make this basic to serve as a starting point. I tweaked a few things to improve validation but using this method you can pass the price, qty, and any other variables necessary for the checkout process. Be careful of URL injections so if you are passing all the parameters consider serializing and unserializing the variables. For more information check out these articles.

http://us2.php.net/manual/en/function.serialize.php
http://us2.php.net/manual/en/function.unserialize.php


As for showing the display of the cart contents, I left that part out since each person will probably be managing that aspect in a specific way. A simple foreach statement will iterate through the cart. A few notes to help with this...

$itemarray = array_keys($cart) will give us our items array

foreach($itemarray as $itemid)
will iterate our item id's

$cart[$itemid] will give you the quantity.

For a visual representation of your cart while learning or debugging consider using the var_dump()

Let me know if you have any questions. Thanks for checking this tutorial out.



Brian J. has been involved in web design since 1997. He is the founder of True Vision Computer Services, Inc. His recent focus has been on web applications and information systems development.

No comments: