Creating a shopping cart webshop with PHP
First off, remember to use session_start() to initiate a new session in the first line of every PHP file that will be used for the webshop.
Adding new product to the shopping cart
The first and foremost important function is addToCart, this function creates a new order line into the cart Array.
function addToCart() {
// count at what order_line number we currently are and increment with 1 for our next line
$order_line_id = count($_SESSION['cart']) + 1;
$_SESSION['cart'][$order_line_id]['product_id'] = $_POST['product_id']; // unique product id, should be taken from your DB
$_SESSION['cart'][$order_line_id]['pieces'] = $_POST['pieces'];
$_SESSION['cart'][$order_line_id]['price'] = $_POST['price']; // price for each product
}
Now you might realise that what you are seeing when you are viewing the shopping cart page of most webshops and it's just outputting a visual representation of all the elements in the cart session array.
The sessions array(shopping cart) with 2 products will now look something like this :
$_SESSION['cart'][1]['product_id'] = 2569; $_SESSION['cart'][1]['pieces'] = 2; $_SESSION['cart'][1]['price'] = 15.25; $_SESSION['cart'][2]['product_id'] = 1973; $_SESSION['cart'][2]['pieces'] = 1; $_SESSION['cart'][2]['price'] = 9.42;
To not get ahead of ourselves, we first have to create a form to order products in which we can call the function addToCart().
<?php
if(isset($_POST['submit'])) { // did the user press a submit button?
addToCart(); // run the addToCart function to add the form information to the session array.
}
?>
<form action="thispage.php" method="post">
Product : 2569, cost : 15.25 dollar per unit.
<input type="submit" name="submit" value="order">
</form>
Each time we create a new product we create this previous form, make sure no forms overlap with eachother.Make sure the addToCart function is included before checking if the user pressed submit.
Showing all products in the shopping cart
When a user ordered a product they will most likely check if the product was succesfully added to the cart.
Sidenote : Include your shopping cart in every posisible page of your webshop, this boosts sales quite a bit.
The folowing example loops through all the order line items in the current session for that user.
$total_order_price = 0;
for($i = 0; $i <= count($_SESSION['cart']); $i++) {
$line_price = $_SESSION['cart'][$i]['pieces'] * $_SESSION['cart'][$i]['price'];
echo "Product id : " . $_SESSION['cart'][$i]['product_id'] . "<br>";
echo "Pieces : " . $_SESSION['cart'][$i]['pieces'] . "<br>";
echo "Price : " . $_SESSION['cart'][$i]['price'] . "<br>";
echo "Total price : " . $line_price . "<br><br>";
$total_order_price = $total_order_price + $line_price;
}
echo "Totale order price : ". $total_order_price;
With the supplied product_id we can fetch the real productname from our database. We calculate the total price of each order line and the entire cart.
Posted by James on 2009-06-22 in the category " php "
