How to Create a Shopping Cart in PHP

By CampCodes Administrator

Updated on:

shopping cart in php

Today, we will create a shopping cart application in PHP. This is one of the needed application if you wanted to create an e-commerce website.

File Structure for simple shopping cart in PHP with source code

index.php : This file will contain all coding part
config.php: This file is used for database connection
style.css: This is a CSS file used for designing purpose
product-images folder:  Used to store the product images

Step1:

Create a database then create a table inside that database tblproducts and insert some data into this table.

tblproducts table structure

--
-- Table structure for table `tblproduct`
--
CREATE TABLE `tblproduct` (
`id` int(8) NOT NULL,
`name` varchar(255) NOT NULL,
`code` varchar(255) NOT NULL,
`image` text NOT NULL,
`price` double(10,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `tblproduct`
--
INSERT INTO `tblproduct` (`id`, `name`, `code`, `image`, `price`) VALUES
(1, 'MSI GF63 Thin Core i7 9th Gen', 'MSI4353', 'product-images/msi-laptop.jpeg', 1500.00),
(2, 'WD 1.5 TB Wired External Hard Disk Drive (Black)', 'WD091', 'product-images/external-hardidisk.jpeg', 50.00),
(3, 'VERTIGO Running Shoes For Men (Black)', 'LOTTO215', 'product-images/lotto-shoes.jpeg', 10.00);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tblproduct`
--
ALTER TABLE `tblproduct`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `product_code` (`code`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `tblproduct`
--
ALTER TABLE `tblproduct`
MODIFY `id` int(8) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Step 2 :

Fetch the inserted products

<div id="product-grid">
<div class="txt-heading">Products</div>
<?php
$product= mysqli_query($con,"SELECT * FROM tblproduct ORDER BY id ASC");
if (!empty($product)) {
while ($row=mysqli_fetch_array($product)) {
?>
<div class="product-item">
<form method="post" action="index.php?action=add&pid=<?php echo $row["id"]; ?>">
<div class="product-image"><img src="<?php echo $row["image"]; ?>"></div>
<div class="product-tile-footer">
<div class="product-title"><?php echo $row["name"]; ?></div>
<div class="product-price"><?php echo "$".$row["price"]; ?></div>
<div class="cart-action"><input type="text" class="product-quantity" name="quantity" value="1" size="2" /><input type="submit" value="Add to Cart" class="btnAddAction" /></div>
</div>
</form>
</div>
<?php
}
} else {
echo "No Records.";
}
?>
</div>

Step 3 :

Adding products into the shopping cart.

//code for adding product in cart
case "add":
if(!empty($_POST["quantity"])) {
$pid=$_GET["pid"];
$result=mysqli_query($con,"SELECT * FROM tblproduct WHERE id='$pid'");
while($productByCode=mysqli_fetch_array($result)){
$itemArray = array($productByCode["code"]=>array('name'=>$productByCode["name"], 'code'=>$productByCode["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode["price"], 'image'=>$productByCode["image"]));
if(!empty($_SESSION["cart_item"])) {
// searches for specific value code
if(in_array($productByCode[0]["code"],array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["code"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}
}
} else {
//The array_merge() function merges one or more arrays into one array.
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
}
break;

The above code used for  adding products in to cart. In this case “add” handle the add cart action. All cart values will be  store in cart session.

READ ALSO:   Auto Search Using jQuery in PHP

Step 4 :

Retrieve cart products from cart session

<!-- Cart ---->
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart</div>
<a id="btnEmpty" href="index.php?action=empty">Empty Cart</a>
<?php
if(isset($_SESSION["cart_item"])){
$total_quantity = 0;
$total_price = 0;
?>
<table class="tbl-cart" cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th style="text-align:left;">Name</th>
<th style="text-align:left;">Code</th>
<th style="text-align:right;" width="5%">Quantity</th>
<th style="text-align:right;" width="10%">Unit Price</th>
<th style="text-align:right;" width="10%">Price</th>
<th style="text-align:center;" width="5%">Remove</th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
$item_price = $item["quantity"]*$item["price"];
?>
<tr>
<td><img src="<?php echo $item["image"]; ?>" class="cart-item-image" /><?php echo $item["name"]; ?></td>
<td><?php echo $item["code"]; ?></td>
<td style="text-align:right;"><?php echo $item["quantity"]; ?></td>
<td style="text-align:right;"><?php echo "$ ".$item["price"]; ?></td>
<td style="text-align:right;"><?php echo "$ ". number_format($item_price,2); ?></td>
<td style="text-align:center;"><a href="index.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction"><img src="icon-delete.png" alt="Remove Item" /></a></td>
</tr>
<?php
$total_quantity += $item["quantity"];
$total_price += ($item["price"]*$item["quantity"]);
}
?>
<tr>
<td colspan="2" align="right">Total:</td>
<td align="right"><?php echo $total_quantity; ?></td>
<td align="right" colspan="2"><strong><?php echo "$ ".number_format($total_price, 2); ?></strong></td>
<td></td>
</tr>
</tbody>
</table>
<?php
} else {
?>
<div class="no-records">Your Cart is Empty</div>
<?php
}
?>
</div>

Step 5 :

Removing the products from cart

// code for removing product from cart
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;

We use unset() to deleting product from cart.

Step 6 :

In this step empty the cart in one click.

 // code for if cart is empty
case "empty":
unset($_SESSION["cart_item"]);
break;

Happy Coding!

Download Here

Next Step for Your Project Journey

Pick your next action to keep building: download complete source code, continue with practical tutorials, or explore more project systems.

Explore Source Code Continue Tutorials Final Year Projects
2020 best php project 2020 best php projects 2020 best php projects to download 2020 download php projects 2020 php projects 2021 best php projects to download 2021 download php projects 2021 php projects all download in php all free download projects in php all free download projects in php 2020 all free download projects in php 2021 all php projects all php projects 2020 all php projects 2021 all php projects download with source code all php projects download with source code 2020 all php projects download with source code 2021 all php projects source code download all php projects source code download 2020 all php projects source code download 2021 all projects of php best php projects to download best php projects to download 2020 best php projects to download 2021 best projects for php best projects for php 2020 best projects for php 2021 best projects to download 2020 best projects to download in 2020 best projects to download in 2021 create a shopping cart in php download all projects in php download all source code in projects of php download best php projects download best php projects 2020 download best php projects 2021 download best php projects with source code download best php projects with source code 2020 download best php projects with source code 2021 download projects for php download projects in php download projects in php 2020 download projects with php download shopping cart in php download source code for php projects download source code with php projects free download php projects free download source code php projects free projects in php free projects in php 2020 free projects in php 2021 free projects with php free projects with php 2020 free projects with php 2021 get php projects get php projects 2020 get php projects 2021 how to create a shopping cart php best projects php best projects 2020 php best projects 2021 php best projects download php best projects download source code php best projects to download php download source code 2020 php download source code 2021 php download source code projects php full source code php full source code 2020 php full source code 2021 php full source code projects php full source code projects 2020 php full source code projects 2021 php full source code projects download php full source code projects download 2020 php get all projects php project php project free download php project free download source code php project free source code php project source code php project source code download 2020 php project source code download 2021 php projects php projects 2020 php projects 2021 php projects all free download php projects all free download 2020 php projects all free download 2021 php projects all free download source code php projects all free download source code 2021 php projects download source code 2020 php projects download source code 2021 php projects free download php projects free download source code php projects free source code php projects free to download php projects free to download 2020 php projects free to download 2021 php projects free to download source code php projects free to download source code 2020 php projects free to download source code 2021 php projects full source code download php projects with demo php projects with full source code 2020 php projects with full source code 2021 php projects with full source code download php projects with full source code download 2020 php projects with full source code download 2021 php projects with source code php projects with source code 2020 php projects with source code 2021 php shopping cart php shopping cart download source code php shopping cart source code php shopping cart source code download php show image php tutorials projects of php download projects to download for php projects to download in php projects to download with php projects to download with php 2020 projects to download with php 2021 projects to download with php source code projects to download with php source code 2020 projects to download with php source code 2021 projects to learn for php projects to learn in php projects to learn with php sample php projects shopping cart shopping cart in php download source code shopping cart in php source code shopping cart in php source code download shopping cart php simple shopping cart in php source code site to download php projects site to download php projects 2020 site to download php projects 2021 source code of shopping cart in php website to download php projects website to download php projects 2020 website to download php projects 2021 website to download php projects for free website to download php projects for free 2020 website to download php projects for free 2021 website to download php projects with free source code website to download php projects with free source code 2020 website to download php projects with free source code 2021

Leave a Comment