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.
I have created 4 files for our shopping cart:
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, OR there’s also a mysql file in downloadable zip, which you can import in database to get the table and the product details.
Once you create the table, you can create a list of products, similar to picture below:
Main purpose of the config file is to store different information we need for our script. Just enter the MySql details in the configuration file.
This will be the page where you want to display the list the products you want to sell to buyers. If you have lots of 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 later.
Since we are working with sessions, we must activate PHP session in our script by including session_start() on top of the code, then we can start writing codes or include file:
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.
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.
The role 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 and quantity 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"] with new array variables. Removing item works similar way, please go though comment lines below, most of the code is self explanatory.
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.