Shopping cart processing with PHP

  • Favourite
Average Star Rating is 3 from the Total 13 Ratings

Let's create a simple php based shopping cart, in this example, I am using two-dimensional array to store my product details. This is a basic php skeleton with three functions get_total(); add_vat() and apply_discount(); third one can be optional as not all products will have discount. Later we will add our UI to display the receipt with bit of a help from Bootstrap CSS.



$cart_products = array(
        array("name"=>"Laptop","quantity"=>1, "price"=>299.99),
        array("name"=>"Desktop","quantity"=>1, "price"=>499.99),
        array("name"=>"Pad","quantity"=>1, "price"=>99.99) 

$subtotal = 0;
foreach($cart_products as $item ){
    $total_amount = get_total($item['quantity'], $item['price']);
    echo $item['name'].", £". $total_amount."<br />";
    $subtotal += $total_amount;

$total_after_discount  =  apply_discount($subtotal,10);
$discount_amount = $subtotal - $total_after_discount;
$grand_total = add_vat($total_after_discount,20);
$vat_amount = $grand_total-$total_after_discount;

echo "<br /><hr/>Subtotal : £".round($subtotal,2)."<br /><hr />
      10% Discount : £".round($discount_amount)."<br/>
      Total after discount £".round($total_after_discount,2)."<br />
      20% VAT : £".round($vat_amount,2)."<br />
      Grand Total £".round($grand_total,2)."(Inc 20% VAT)<br/>";

function get_total($quantity, $price){
    $total_amount = $quantity * $price;
    return $total_amount;

function add_vat($total_amount,$percentage){
    $percentage = $percentage /100;
    return $total_amount + ($total_amount * $percentage );

function apply_discount($total_amount, $percentage){
    $percentage = $percentage /100;
    return $total_amount - ($total_amount * $percentage );



Adding UI : For simplicity, we can keep these two blocks of code in one file, just comment out any echo from php script.  I am using a UI template form Please see the completed demo of the shopping_cart or have a look at screenshot below. 


<!DOCTYPE html>
<html lang="en">
<title> PHP Shopping cart </title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="" rel="stylesheet" id="bootstrap-css">
<script src=""></script>
<script src=""></script>
<div class="container" style="margin: 50px auto;">
    <div class="row">
    <div class="well col-lg-6 col-sm-6 col-sm-offset-3">
        <div class="row">
            <div class="text-center">
            <table class="table table-hover">
                        <th class="text-center">Price</th>
                        <th class="text-right">Total</th>
            <?php foreach($cart_products as $item ):?>
                    <tr >
                        <td class="col-md-9"><em><?=$item['name'];?></em></h4></td>
                        <td class="col-md-1" style="text-align: center"> <?=$item['quantity'];?></td>
                        <td class="col-md-1 text-center"><?="£".$item['price'];?></td>
                        <td class="col-md-1 text-right"><?="£".get_total($item['quantity'],$item['price'] )?></td>
            <?php endforeach; ?>  
                        <td class="text-right" colspan="3" >
                                <p><strong>Subtotal : </strong></p>
                                <p><strong>10% Discount : </strong></p>
                                <p><strong>Total before VAT : </strong></p>
                                <p><strong>VAT : </strong></p>
                        <td class="text-right">
                                <p><strong><?= "£".round($subtotal,2)?></strong></p>
                                <p><strong><?="- £".round($discount_amount,2) ;?></strong></p>
                        <td class="text-right" colspan="3">
                        <h4><strong>Grand Total: </strong></h4></td>
                        <td class="text-right text-danger"><h4><strong><?="£".round($grand_total,2);?></strong></h4></td>
            <button type="button" class="btn btn-success btn-lg btn-block">
                Pay Now   <span class="glyphicon glyphicon-chevron-right"></span>


Output :

   shopping_cart (1)  

user profile image
On February 12, 2019

I hope this is going to help me with our next assignment.

user profile image
On February 12, 2019

You are right Viacheslav, it certainly will help!