Simple PHP Shopping Cart

In this tutorial we are going to see a simple PHP example for creating shopping cart application. This shopping cart application is purposely kept as simple as possible. You can download this and quickly customize it for your needs. In this example, we are displaying list of products from database. For each product, we have given options for selecting product quantity and adding to cart. The cart items are stored in a session. We can clear this session by removing items from the cart.

php-shopping-cart

View Demo

Adding Products  to Shopping Cart

This code retrieves list of products from the database and displays in a grid with add to cart option.

<?php
$product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
if (!empty($product_array)) { 
foreach($product_array as $key=>$value){
?>
<div class="product-item">
	<form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
	<div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>"></div>
	<div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>
	<div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
	<div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>
	</form>
</div>
<?php }} ?>

While adding products to the shopping cart, the following switch case will be executed to store selected items information into the session. If the selected item is already in session (in the cart), then the quantity will be checked whether it is changed. If so, it will be updated.

case "add":
	if(!empty($_POST["quantity"])) {
		$productByCode = $db_handle->runQuery("SELECT * FROM tblproduct WHERE code='" . $_GET["code"] . "'");
		$itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));		
		if(!empty($_SESSION["cart_item"])) {
			if(in_array($productByCode[0]["code"],$_SESSION["cart_item"])) {
				foreach($_SESSION["cart_item"] as $k => $v) {
					if($productByCode[0]["code"] == $k)	$_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
				}
			} else $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
		} else 	$_SESSION["cart_item"] = $itemArray;
	}
break;

Removing or Clearing Cart Item

In this example we have two more cases to deal with the shopping cart operations like removing specific cart item and emptying the cart session completely. The code is,

case "remove":
	if(!empty($_SESSION["cart_item"])) {
		foreach($_SESSION["cart_item"] as $k => $v) {
			if($_GET["code"] == $k)	unset($_SESSION["cart_item"][$k]);				
			if(empty($_SESSION["cart_item"])) unset($_SESSION["cart_item"]);
		}
	}
break;
case "empty":
	unset($_SESSION["cart_item"]);
break;

Shopping Cart Database SQL script

CREATE TABLE IF NOT EXISTS `tblproduct` (
  `id` int(8) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `code` varchar(255) NOT NULL,
  `image` text NOT NULL,
  `price` double(10,2) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `product_code` (`code`)
)

View DemoDownload

This PHP code tutorial was published on October 26, 2014.

↑ Back to Top