- ★
- ★
- ★
- ★
- ★
PHP
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.
<?php
$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 Bootsnipp.com. Please see the completed demo of the shopping_cart or have a look at screenshot below.
<!DOCTYPE html>
<html lang="en">
<head>
<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="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
</head>
<body>
<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">
<h1>Receipt</h1>
</div>
</span>
<table class="table table-hover">
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th class="text-center">Price</th>
<th class="text-right">Total</th>
</tr>
</thead>
<tbody>
<?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>
</tr>
<?php endforeach; ?>
<tr>
<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>
<td class="text-right">
<p><strong><?= "£".round($subtotal,2)?></strong></p>
<p><strong><?="- £".round($discount_amount,2) ;?></strong></p>
<p><strong><?="£".round($total_after_discount,2);?></strong></p>
<p><strong><?="£".round($vat_amount,2)?></strong></p>
</td>
</tr>
<tr>
<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>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-success btn-lg btn-block">
Pay Now <span class="glyphicon glyphicon-chevron-right"></span>
</button></td>
</div>
</div>
</div>
</body>
</html>
Output :
You are right Viacheslav, it certainly will help!
On February 12, 2019
I hope this is going to help me with our next assignment.