- Simple PHP Shopping Cart
- How does PHP Shopping Cart work?
- Products listing
- Adding product to shopping cart
- Removing one or empty all from cart
- Reader Interactions
- Comments
- Leave a Reply Cancel reply
- Making a shopping cart system with PHP 7.4 and MySQL
- Product detail page and add to cart form
- Adding to the cart
- Cart page and checkout
- Cart item removal
- Cart items display
Simple PHP Shopping Cart
In this tutorial, we are demonstrating how to make a simple PHP shopping cart tutorial step by step from scratch. This shopping cart application wrote as very minimal and simple for learning purpose. One can take this as an easy shopping cart for any website, but this is purely an idea to create a shopping cart website. More complex websites need a lot of other things like payment gateways, shipping methods, etc. This tutorial will give step by step approach to build a shopping cart with PHP and mysql and this php e-commerce script can be used pretty much any level of programmer in php.
Do not use this directly in the production system without making necessary security approaches. This example has a certain number of products, displaying from the database.
We can add each and every product to cart. With help of session, products are storing in the cart. So cart will be empty when we clear the session.
How does PHP Shopping Cart work?
Products listing
First, we are creating the product catalog listing grid. We have all our products in the database with name, price, and SKU. In the below listing we are getting products from the database and iterate using a foreach loop. Just remember this tutorial using sessions to store the user selected product details.
Each product has an add to cart button in order to add the product into session cart.
Adding product to shopping cart
When the user presses the Add to Cart button the product details saving into the cart session with SKU and other details. The following code has the action add to cart and it adds the product to shopping cart session.
If we click on the same product several time, the quantity will be incremented.
Removing one or empty all from cart
We allow users to individually remove the item from the cart by pressing the button Delete. Either they can press Empty Cart button to delete all products in one go.
We use unset() function in php to delete the session form cart.
You can download the simple shopping cart in php source code by clicking the below download button
Note: When you learn how to write a simple ecommerce shopping cart with php and mysql, give a try to ecommerce content management systems such as Magento , Open Cart .They are some of the best shopping cart solutions out there in the open source world. And also they equipped with payment solutions such as Stripe, PayPal etc, which gives you hassle-free configuration options of your store.
Hope you have enjoyed the article about how to build a shopping cart website in simple PHP, please don’t hesitate to share with your friends also don’t hesitate to comment about this simple shopping cart system with php and mysql.
Editorial Staff
Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.
Reader Interactions
Comments
hey. thanks for this awesome tutorial! do you have any videos or tutorials about paypal integration or popular payment gateway options for beginners?? thanks 🙂
Glad you liked it. At the moment we don’t have the article requested, but simple google search you can find a lot of articles.
I wish you had a live demo. Your sample image shows a total bill of $500 for a thousand dollars worth of merchandise (two @ $400 & two @ 100)
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Making a shopping cart system with PHP 7.4 and MySQL
This code example is just a demo to show what is possible, and should not be used for actual e-commerce websites. let’s assume that you already have a product page and are displaying these products from a MySQL database, and now you want to make a cart system so a user can purchase more than one item at a time.
Product detail page and add to cart form
This page show and individual item from the products page and will contain a form to add this item to the cart of the website. This form will go to an addtocart.php file that will set the cookie for the cart. The form will look something like the below code. The ID for this form would have been grabbed from a MySQL database.
print(" $id' method='post'> ");
Adding to the cart
Now for the addtocart.php file. For this page, you will want to connect your MySQL server to get the items from the database to add to the cart. You will also want to make sure that you tell PHP that you want to start using sessions with the session start function.
$server = "localhost"; $username = "username"; $password = "123456789"; $database = "e-commerce"; $connection = mysqli_connect($server, $username, $password, $database); /* And just in case something goes wrong */ if(!$connection) die("Connection Failed: " . mysqli_connect_error()); >
Once you have connected to the server we can add the item to the session. Because of the detail page form, the URL should look something like this example.com/addtocart.php?id=1 which means we can grab the ID of the item from the current URL. Even though the detail page form has the method of post we can still use GET to grab the id because we added it to the action of the form.
$id = $_GET['id']; // Check if the add to cart button was clicked or someone just wants to load this page. This POST method `add` is from the id of the add to cart button on the detail page. if(isset($_POST['add'])) // Check if there is a session for the cart set already, if not then set one. if(!isset($_SESSION['shoppingcart'])) $_SESSION['shoppingcart'] = []; > $quantity = $_POST['quantity']; $item = [ "id" => $id, "quantity" => $quantity ]; // Now add new item to the cart array_push($_SESSION['shoppingcart'], $item); // Once added let's take the user to the cart page header("Location: cart.php"); > else // If the add to cart button was not clicked then go back to the products page. header("Location: products.php"); >
Cart page and checkout
This page will display all of the items in the cart, have a checkout section, and remove all of the cart items.
Cart item removal
To remove all of the items from the cart let’s start with the function that will check if the customer would like to do so and add the form that will trigger this action.
if(isset($_GET['reset'])) session_start(); unset($_SESSION['shoppingcart']); session_destroy(); > // Start the session for the rest of the page after destroying it. session_start();
For the form that will remove all of the items, it would look like this. This form will simply reload this file and add ?reset=RESET to the URL activating the PHP script above to remove the items.
action="#" method="GET"> for="reset">Reset cart: type="submit" value="RESET" name="reset" id="reset" />
Cart items display
// First check if the session is set is(isset($_SESSION['shoppingcart']) && count($_SESSION['shoppingcart'])) $list = $_SESSION['shoppingcart']; foreach($list as $item) $id = $item['id']; // The MySQL database query for the items in the cart. $query = "SELECT * FROM products WHERE "; // The connection variable is taken from the top of the page when we connected to the MySQL database $result = mysqli_query($connection, $query); // If only on item in the database has this id then proceed if(mysqli_num_rows($result) == 1) $row = mysqli_fetch_array($result); $id = $row['id']; $name = $row['name']; $price = $row['price']; $quantity = $item['quantity']; // Now let's print out this item to HTML print( " $id'> $name $$price CAD
Quantity:
$quantity "); > > > else print("You don't have any items in your shopping cart.
"); >