Register Now

Login

Lost Password

Enter your email to reset your password.

Simple PHP & Mysql Shopping Cart

Let’s us learn how to build a PHP Shopping Cart. It is very easy and simple. You can easily download this free and customize as per your need.

This software is well-designed with the help of CSS, and Bootstrap.

PHP Shopping Cart Software – has the following functionality:

  • Retrieve products information (product name, product price and image) from the database.
  • Create product gallery in front-end with the help of bootstrap.
  • Addition, deletion of cart items using the PHP Session.
  • Number of products in cart.

File Structure

Following files have been used for this software.

  1. db.php – It contains the code for database connectivity.
  2. index.php – to display the products for the shopping cart.
  3. cart.php – contains code for add to cart and remove a product from cart.
  4. product-images – contains the image of products
  5. bootstrap/bootstrap.min.css – bootstrap Css template.
  6. bootstrap/bootstrap.min.js – bootstrap Js file
  7. js/jquery.js – Jquery file

Let’s go through the each block of code step by step to understand it better.

Step 1: Create products gallery

In order to create a product gallery, I have used the PHP script to get the products information from the database. In product gallery, I’ve shown products information like product name, product image, product price.

<?php
$query = "SELECT * FROM tblproduct ORDER BY id ASC";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_array($result)) {
        ?>
        <div class="col-md-4">
            <div class="well text-center center-block" style="min-height:350px;">
                <form method="post" action="index.php?action=add&id=<?php echo $row['id']; ?>">
                    <center><img src="<?php echo $row['image']; ?>" class="img-responsive" style="width:300px;height:180px;"></center><br>
                    <h4 class="text-info"> <strong><?php echo $row['name']; ?> </strong></h4>
                    <h4 class="text-danger"> <strong>$ <?php echo $row['price']; ?></strong></h4>
                    <input type="number" name="quantity" class="form-control" value="1"/>
                    <input type="hidden" name="hidden_name" value="<?php echo $row['name']; ?>"/>
                    <input type="hidden" name="hidden_price" value="<?php echo $row['price']; ?>"/><br>
                    <input type="submit" name="add_to_cart" class="btn btn-lg  btn-primary" value="Add to Cart" />
                </form>
            </div>
        </div>
        <?php
    }
}
?>

Step 2: Adding a product to cart

Once the product gallery is created. I’ve written code for adding a product to shopping cart. Shopping cart is a block where user can get information about the product he/she choose. Also it will show the product quantity and price for the added item in the cart.

<div class="col-md-12">
    <h3>Order Details</h3>
    <div class="table-responsive">
        <table class="table table-bordered table-striped">
            <tr>
                <th>Product Name</th>
                <th>Quantity</th>
                <th>Price</th>
                <th>Total</th>
                <th>Action</th>
            </tr>
            <?php
            if (!empty($_SESSION["shopping_cart"])) {
                $total = 0;
                foreach ($_SESSION["shopping_cart"] as $key => $values) {
                    ?>
                    <tr>
                        <td><?php echo $values["item_name"]; ?></td>
                        <td><?php echo $values["item_quantity"]; ?></td>
                        <td><?php echo $values["item_price"]; ?></td>
                        <td><?php echo number_format($values["item_quantity"] * $values["item_price"], 2); ?></td>
                        <td><a href="index.php?action=delete&id=<?php echo $values["item_id"]; ?>"><span class="btn btn-danger">Remove</span></a></td>
                    </tr>
                    <?php
                    $total = $total + ($values["item_quantity"] * $values["item_price"]);
                }
                ?>
                <tr>
                    <td colspan="3" align="right"> Total</td>
                    <td align="right">$ <?php echo number_format($total, 2); ?></td>
                </tr>
                <?php
            }
            ?>
        </table>
    </div>
</div>
<?php

//Adding an Item to Cart
if (isset($_POST['add_to_cart'])) {
    if (isset($_SESSION['shopping_cart'])) {
        $item_array_id = array_column($_SESSION['shopping_cart'], 'item_id');
        if (!in_array($_GET['id'], $item_array_id)) {
            $count = count($_SESSION['shopping_cart']);
            $item_array = array(
                'item_id' => $_GET['id'],
                'item_name' => $_POST['hidden_name'],
                'item_price' => $_POST['hidden_price'],
                'item_quantity' => $_POST['quantity']
            );
            $_SESSION["shopping_cart"][$count] = $item_array;
        } else {
            echo '<script>alert("Item Already Added")</script>';
            echo '<script>window.location="index.php"</script>';
        }
    } else {
        $item_array = array(
            'item_id' => $_GET['id'],
            'item_name' => $_POST['hidden_name'],
            'item_price' => $_POST['hidden_price'],
            'item_quantity' => $_POST['quantity']
        );
        $_SESSION['shopping_cart'][0] = $item_array;
    }
}

Step 3: Removing a product from cart

To remove a product from cart, php “get” method is used. Using Get method, you’ll get the product id and then you can unset that product id from array of the shopping cart session. Once it is removed, product cart is updated and return the updated information.

//Delete an Item from cart
if (isset($_GET["action"])) {
    if ($_GET["action"] == "delete") {
        foreach ($_SESSION["shopping_cart"] as $key => $values) {
            if ($values["item_id"] == $_GET["id"]) {
                unset($_SESSION["shopping_cart"][$key]);
                echo '<script>alert("Item Removed!")</script>';
                echo '<script>window.location="index.php"></script>';
            }
        }
    }
}

Step 4: Shopping Cart Output

Below is the screenshot attached. Shopping cart will look like this. You can modify the look and functionality as per your own requirement.

shopping-cart

Leave a reply