Demo of using session array to create a simple shopping cart

 Shopping cart plus2-cart-v1 We will be using array commands to manage our shopping cart. We will be using three files.

cart.php : to Declare the array and adding elements
cart-display : Displaying all elements of the array
cart-remove-all: using unset command cart can be cleared.

At the starting of each page we will keep session_start() command. Here is the code for cart.php file
<?Php
session_start();
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Demo of Session array used for cart from plus2net.com</title>
</head>
<body>

<?Php
$_SESSION['cart']=array(); // Declaring session array
array_push($_SESSION['cart'],'apple','mango','banana'); // Items added to cart

echo "Number of Items in the cart = ".sizeof($_SESSION['cart'])." <a href=cart-remove-all.php>Remove all</a><br>";
?>
</body>
</html>
In the above code we used array_push() to add products to our shopping cart array. We have also used sizeof to count total number of elements present in our array.

cart-display.php :Displaying elements ( products )

We can display all elements or products present inside the array cart by using while loop. Here is the code.
<?Php
session_start();
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>

<head>
<title>Displaying Session Cart products from plus2net.com</title>
</head>

<body>
<?Php
echo "Number of Items in the cart = ".sizeof($_SESSION['cart'])." <a href=cart-remove-all.php>Remove all</a><br>";

while (list ($key, $val) = each ($_SESSION['cart'])) { echo "$key -> $val <br>"; } ?> </body> </html>

cart-remove-all.php : Remove all elements ( products ) from the cart

We will use unset command to remove elements from the cart. After removal we will display the total number of products available in the cart.
<?Php
session_start();
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>

<head>
<title>Session Cart removal by plus2net.com</title>
</head>

<body>
<?Php

while (list ($key, $val) = each ($_SESSION['cart'])) { 
//echo "$key -> $val <br>"; 
unset($_SESSION['cart'][$key]);
}

echo "Number of Items in the cart = ".sizeof($_SESSION['cart'])." <br>";

?>
</body>
</html>
In above code we are removing items from the cart and not removing the cart itself. By using the code below we can completely remove the cart.
unset($_SESSION['cart']);
After removing the cart if you try to display the items then script will give a warning message like this.
PHP Warning:  Variable passed to each() is not an array or object in J:\..path....\cart-display.php on line 15
You can keep the code block inside isset() and then use.
if(isset($_SESSION[cart])){
// script to check and display elements 
}else{
// message to user to add products to cart
}

cart-remove.php Removing elements based on user selection

We can add a feature to remove elements from the cart based on user selection. For this we need to add one more page. We will say cart-remove.php . For this we need to understand handling of checkbox array and getting the checkbox array data after the form is posted.
<?Php
session_start();
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>

<head>
<title>Session Cart removal on selection by user at plus2net.com</title>
</head>

<body>
<?Php
$item=$_POST['item'];
while (list ($key1,$val1) = @each ($item)) {
//echo "$key1 , $val1,<br>";
unset($_SESSION['cart'][$val1]);

}

echo "Number of Items in the cart = ".sizeof($_SESSION['cart'])." <br>";
echo "<form method=post action=''>";
while (list ($key, $val) = each ($_SESSION['cart'])) { 
echo " <input type=checkbox name=item[] value='$key'>  $key -> $val <br>"; 
}
echo "<input type=submit value=Remove></form>";
?>
<a href=cart.php>Cart adding</a> . <a href=cart-display.php>Display Items</a> .<a href=cart-remove.php>Remove Item</a> 
</body>
</html>

Adding product by user to the cart

User can enter product name and add them to cart. For this a new file card-add.php is added to the script. This page shows a text box to user and on submit the data is added to shopping cart. This way users can add items to the cart without affecting the existing items in the cart.
<form method=post action=''>
Enter a product name <input type=text name=product>
<input type=submit value='Add to Cart'>
</form>
<?Php
@$product=$_POST['product'];
if(strlen($product)>3){
array_push($_SESSION['cart'],$product); // Items added to cart
}
echo "<br>Number of Items in the cart = ".sizeof($_SESSION['cart']);

Session Cart with Multidimensional array

Products can have more than one attributes for the users to select. User can select quantity or colour or size while selecting a product. We will modify the above script and use multidimensional array as session variable.

You can learn how to add , remove , products to a session array here.

You can download the demo script with multidimensional array here .




Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    hari

    13-12-2014

    good tutorial :) Thanks!
    Artur

    16-02-2015

    Hallo, good tutorial to think, but I think it's kind of bad tutorial aswell, if you will use it in real shop there will be problem with array_push, for example array_push($_SESSION['cardItems'],$productID); will overwrite last added product.
    luigi

    23-03-2015

    Now that's a very useful and thank God finally a simple code, thanks!
    snehal

    31-10-2015

    how to database calling in array for cart page .
    Furqan

    04-09-2018

    how selected shopping cart item show list in cart on next page on click cart(basket) using javascript and used session without database
    smo1234

    05-09-2018

    @Artur : array_push() will not remove any element from the array, it will only add at the end of the array.

    19-04-2021

    Amazing stuff




    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer