Creating Simple Shopping Cart with PHP
Shopping Cart is very important part of any online store, it not only lets users to accumulate list of items for final purchase, but also calculates a total amount for the order. Since my last article on PayPal Express Checkout with PHP, I have received few emails from readers asking how to create simple PHP session based shopping cart for the website. So, keeping that in mind, today we will create a simple but working online shopping cart step by step, which can be integrated easily in any website that runs on PHP.
Mainly I have created 4 files for our shopping cart:
- Config.php (MySql credentials and other settings)
- Index.php (list of product information from database)
- Cart_update.php (Updates cart and redirects back to products list page)
- View_cart.php (Product summery before payment)
All our products information should be stored in MySql table, lets create a table named “products”, you can insert the products details manually using PhpMyAdmin, there’s also a mysql file in downloadable zip, which you can easily import in database to get the table and the product details.
MySql table should create a list similar to picture below:
Main purpose of the config file is to store different information we need for our script. Enter the MySql details in the configuration file.
Product List (Index.php)
This will be the main page where users get to see the products they want to purchase, if you have many products, it is better to group them in pages using some sort of pagination. And on the right side of the page will be our shopping-cart box, which will keep track of items user going to purchase.
Since are working with sessions, let’s activate PHP session for our script before we start coding anything, and then we include the config file for the MySql queries.
Listing Products from Database
We will basically fetch the records from database and display them on the page. We will also create a HTML form with Add to Cart button. It doesn’t matter how you are planing to display them, the important part here is the HTML form. Notice the hidden input values? each item contains a form with these hidden values, product code and the return URL, which we will send to cart_update.php using POST method.
Shopping Cart Box
On the right side of the page we will display small shopping cart box to keep track of user items. The main task of the shopping-cart is to look for session variable called $_SESSION["products"], which holds the collection of user items in an array. And then retrieve and display its content in the box, displaying items and total amount.
Main purpose of the Cart_update.php is to add and remove items in the shopping cart. When user clicks “Add to Cart” button, the form sends some hidden values such as product code to Cart_update.php using POST method, which we will use to retrieve product info from the database verifying the existence of the product, and then we create or update $_SESSION["products"]. Removing item works similar way, please go though comment lines below, most of the code is self explanatory.
View Item Summary
Now we have everything ready, its time for final page where user can view their products and proceed to payment. It’s good idea to add taxes, shipping and transaction fees along with total amount in this page for the users to see. You can generate any type of data here for the Payment gateway, most Payment gateway prefer HTML form, so I have create some hidden input fields, you can modify them to suit your needs.
Continue to PHP Shopping Cart to PayPal Payment Gateway.
Saran Chamling. (He loves the web technology, and enjoys designing, exploring and writing about it @saaraan.com).