Hi coders 🙂

This tutorial does not include going from items in the card to payment and then rewarding for the payment. Only setting up the cart and adding items to it.

You are viewing this page because you want a simple cart system for your website store. This tutorial will be quick and easy but in return will provide you with a very basic cart system that should not be used in a business environment, I would only use this if you’re new to coding!

Here’s what you will need:

– Basic knowledge of mysql

– Basic php/html/css knowledge

– User accounts set up and working

 

That’s it! Now lets get to work!

First we’re going to decide on what we’re selling, lets say I’m selling these 5 items for a company:

– Apple, costs £1

– Banana, costs £1.50

– Melon, costs £4

– Orange, costs £0.50

– Mango, costs £2

Now create a new table in mysql called “cart” with 3 colums (id, user_id, item_id), all columns are type “int”.

Once you’ve done this, create a new php file and call it “cart.php”. Now lets create a basic table, nothing fancy. Copy this code:

<h2>Items</h2>
<form method=”POST”>
<table>
<tr>
<td><b>Name</b></td>
<td><b>Price</b></td>
<td></td>
</tr>
<tr>
<td>Apple</td>
<td>£1</td>
add1″ value=”Add to Cart”/>
</tr>
<tr>
<td>Banana</td>
<td>£1.50</td>
add2″ value=”Add to Cart”/>
</tr>
<tr>
<td>Melon</td>
<td>£4</td>
add3″ value=”Add to Cart”/>
</tr>
<tr>
<td>Orange</td>
<td>£0.50</td>
add4″ value=”Add to Cart”/>
</tr>
<tr>
<td>Mango</td>
<td>£2</td>
add5″ value=”Add to Cart”/>
</tr>
</table>
form>

Now you will have a table of your items, you can change the items how you want. Clicking “Add to Cart” won’t do anything yet though!

Lets change that, we want each click on “Add to Cart” to add the item to the cart, to do this we will insert a row into our database, like the following code shows:

 

IF(isset($_POST[‘add1’])){
$itemid = 1;
$itemname = ‘an Apple’;
}elseif(isset($_POST[‘add2’])){
$itemid = 2;
$itemname = ‘a Banana’;
}elseif(isset($_POST[‘add3’])){
$itemid = 3;
$itemname = ‘a Melon’;
}elseif(isset($_POST[‘add4’])){
$itemid = 4;
$itemname = ‘an Orange’;
}elseif(isset($_POST[‘add5’])){
$itemid = 5;
$itemname = ‘a Mango’;
}

IF(isset($itemid)){
$add_to_cart = mysqli_query($con,”INSERT INTO cart (user_id,item_id) VALUES ($userid,$itemid)”);
echo “You have added $itemname to your cart!
“;

}

“$userid” is the ID of the user who is currently logged in. After this, your users will be able to add items to their cart, but they won’t be able to see their cart yet!

Lets make it so they can view their cart, add an optional ”


” after the “form>” if you want, it will just look a bit better. Now put the following code after the “” or ”


” depending on what you put:

<table>
<tr>
<td><b>Name</b></td>
<td><b>Price</b></td>

<td></td>
</tr>
php

echo “<b>Current cart items: </b><br><br>”;

Now lets get the items from the table with this query: $get_cart = mysqli_query($con,”SELECT * FROM cart WHERE cart.user_id = $userid”);

Then we want to check whether or not the cart actually has items, you will do this with the following code:

IF(mysqli_num_rows($get_cart) == 0){
echo “You have no items in your cart.”;
}else{

echo “Will do later”;

}

For the “Will do later”, you want to remove that echo completely now and put this:

$totalprice = 0;
while($row = mysqli_fetch_array($get_cart)){
$itemid = $row[‘item_id’];
$rowid = $row[‘id’];
IF($itemid == 1){
$itemname = ‘Apple’;
$itemprice = 1;
}elseif($itemid == 2){
$itemname = ‘Banana’;
$itemprice = 1.50;
}elseif($itemid == 3){
$itemname = ‘Melon’;
$itemprice = 4;
}elseif($itemid == 4){
$itemname = ‘Orange’;
$itemprice = 0.5;
}elseif($itemid == 5){
$itemname = ‘Mango’;
$itemprice = 2;
}
?>
<tr>
php echo $itemname; ?>
£php echo $itemprice; ?>

php echo $rowid; ?>”/>


</tr>
php
$totalprice = $totalprice + $itemprice;
}
echo “Total price: £”,number_format($totalprice);

What that does is get each row that belongs to that user and put them in one list, as well as the total price. It will also allow users to remove their listings.

And that’s it! Users can add/remove items from their cart and their prices are calculated correctly 😀 From there you can add your payment method how you want, if you use in-site items (like some kind of resource (like Coins, Gold etc) then you can add some queries and get it working with that 🙂 )

WARNING: This is NOT a good payment option, do NOT use this in an actual live site or people will hack their way around it. I am not responsible for anything people do with this code 🙂

 

FULL CODE:

 

php
include(“connect.php”);

$userid = 5;

?>
<h2>Items</h2>

php

IF(isset($_POST[‘add1’])){
$itemid = 1;
$itemname = ‘an Apple’;
}elseif(isset($_POST[‘add2’])){
$itemid = 2;
$itemname = ‘a Banana’;
}elseif(isset($_POST[‘add3’])){
$itemid = 3;
$itemname = ‘a Melon’;
}elseif(isset($_POST[‘add4’])){
$itemid = 4;
$itemname = ‘an Orange’;
}elseif(isset($_POST[‘add5’])){
$itemid = 5;
$itemname = ‘a Mango’;
}

IF(isset($itemid)){
$add_to_cart = mysqli_query($con,”INSERT INTO cart (user_id,item_id) VALUES ($userid,$itemid)”);
echo “You have added $itemname to your cart!
“;

}

?>
<form method=”POST”>
<table>
<tr>
<td><b>Name</b></td>
<td><b>Price</b></td>
<td></td>
</tr>
<tr>
<td>Apple</td>
<td>£1</td>
<td><input type=”submit” name=”add1″ value=”Add to Cart”/></td>
</tr>
<tr>
<td>Banana</td>
<td>£1.50</td>
<td><input type=”submit” name=”add2″ value=”Add to Cart”/></td>
</tr>
<tr>
<td>Melon</td>
<td>£4</td>
<td><input type=”submit” name=”add3″ value=”Add to Cart”/></td>
</tr>
<tr>
<td>Orange</td>
<td>£0.50</td>
<td><input type=”submit” name=”add4″ value=”Add to Cart”/></td>
</tr>
<tr>
<td>Mango</td>
<td>£2</td>
<td><input type=”submit” name=”add5″ value=”Add to Cart”/></td>
</tr>
</table>
</form>

<hr />

<table>
<tr>
<td><b>Name</b></td>
<td><b>Price</b></td>
<td></td>
</tr>
php

echo “<b>Current cart items: </b><br><br>”;
IF(isset($_POST[‘remove’])){
$rowid = $_POST[‘removed_item_id’];
$remove_from_cart = mysqli_query($con,”DELETE FROM cart WHERE cart.id = $rowid”);
echo “<font color=’red’>You have removed that item from your cart!<br></font>”;
}
$get_cart = mysqli_query($con,”SELECT * FROM cart WHERE cart.user_id = $userid”);
IF(mysqli_num_rows($get_cart) == 0){
echo “You have no items in your cart.”;
}else{
$totalprice = 0;
while($row = mysqli_fetch_array($get_cart)){
$itemid = $row[‘item_id’];
$rowid = $row[‘id’];
IF($itemid == 1){
$itemname = ‘Apple’;
$itemprice = 1;
}elseif($itemid == 2){
$itemname = ‘Banana’;
$itemprice = 1.50;
}elseif($itemid == 3){
$itemname = ‘Melon’;
$itemprice = 4;
}elseif($itemid == 4){
$itemname = ‘Orange’;
$itemprice = 0.5;
}elseif($itemid == 5){
$itemname = ‘Mango’;
$itemprice = 2;
}
?>
<tr>
php echo $itemname; ?>
£php echo $itemprice; ?>

rowid; ?>”/>


</tr>
<?php
$totalprice = $totalprice + $itemprice;
}
echo “Total price: £”,number_format($totalprice);
}
?>
</table><br />