­­­IT360 Lab 10: Implementing a Shopping Cart


DUE: March 28, 2013, 2359 (paper copy BEFORE start of lab next day)


This lab should get you familiarized with using session variables.  We will use the database for the online store that you already created in previous labs. In this lab, you will finally create a web interface so customers can purchase the products.



a)     Connect to the cardhu.cs.usna.edu MySQL server by using MySQL Workbench.

b)     Make sure you have the tables for your online store: you can either use you design from previous labs, if that allows products, quantities bought, and sales information to be stored in the database, or execute the script available on the course website, to create the tables that correspond to the following ER diagram: (same tables as in previous lab)



To see the tables created you can use the MySQL “show tables” command in a query tab window. To see the columns of a table, for example to see the columns of the Product table, you can use “describe Product;”. 


c)     Create a Lab10 directory on your W drive. All the files created for this assignment should be stored in the W:/Lab10 folder.  You will be using an object-oriented approach for this lab.  You can copy your files from Lab9 to Lab10 directory, but you do not have to have Lab9 working in order to work on this lab. A solution for Lab9 is also provided on the course website if you want to use it.


Create a shopping cart


The online shopping cart is a specific online shopping mechanism: while the potential customer browses the online catalog of products, he/she can choose products to add to his/her cart. At check-out, the products from the cart are purchased. In order to implement the shopping cart functionality, you need to:

  1. Provide some way for a customer to browse the products in your store, and provide complete information about the products. The products are stored in the MySQL database, so your program needs to get the products from the database (you probably already did that in the previous lab).
  2. Implement a shopping cart to track the products a customer wants to buy. You need to allow users to add products to the cart and to view the content of the cart. When the users view the contents of the cart, complete information about the products should be displayed (barcode, name, price, quantity ordered). You should keep track not only of the products the users want to buy, but also the quantities ordered. You have to use session variables for this task.
  3. Have a checkout script that processes the order. The checkout script needs to ask for payment and delivery information from the customer, record the complete sale information to the database (in both the SALE and SATEITEM tables), display a confirmation message with all the details of the order, and empty the cart. The sale information in the database should include the products bought, quantities, price, delivery address, and payment information. 


You can implement the shopping cart functionality and the interface to your system in any way you want, as long as the above requirements are satisfied and you are using an object-oriented approach using PHP.  If you if want more details, below are detailed steps on how you could implement the shopping cart for your online store.


Details: Here are the UML Class Diagrams for the PHP classes relevant for this lab:



PART 1: Add to cart:


1) As a starting point, you can use productPage.php file from your previous lab or posted solution. If needed, modify productPage.php page to display in a form a list of all products (barcode, name and price), with radio buttons or a checkbox for each product. Add an “Add to cart” button to the form. The action should be “productPage.php”. Below is a sample page. 



2) The purpose of the form is to allow a customer to select the products it wants to add to the cart.


Create a new shoppingCart.ini.php file and create in it a class named ShoppingCart. Create and implement a static function in this class that allows products to be added to the cart:


public static function addToCart($barCode)


Hint: This will use a session variable for example $_SESSION[‘cart’] to store the array of product ids and corresponding quantities (remember the class exercise).


3) Now add code to productPage.php so if the user presses the “Add to Cart” button, you call the addToCart function in ShoppingCart for each of the products selected by the customer. Print some appropriate feedback message to the user and allow the user to add more products to the cart (re-display the products list).


This is how the screen could look like after user added product 2 to cart.



PART 2: View Cart


4) Add a “View cart” button to the form on productPage.php.




5) Add code to productPage.php so if the user clicks on the “View cart” button, the shoppingCartPage.php that you will write next is executed. Hint: you can use the PHP method “header” to re-direct the execution to a specific page. For example, header(Location:shoppingCartPage.php”) will start executing shoppingCartPage.php


6) Create a static listCartProducts() function in the ShoppingCart class that returns a table with all the products in the cart. For each product, the barcode, product name, price and quantity ordered should be displayed. Note that for each product barcode, you probably need to query the database for the name and price corresponding to that product. You can write a method called getProductInfo($db, $barCode) in the Product class to help with this task.


7) Create the shoppingCartPage.php that will display the list of products returned by the listCartProducts() function  into a form. The action for the form should be shoppingCartPage.php. Add a “Checkout” button. Also add a link back to “productPage.php”, in case the customer wants to continue shopping.


Test you program. Add to cart several products, and then see if they are displayed when you click on the “View Cart” button in “productPage.php


This is the shopping cart after adding some items and user clicked on “View Cart” in productPage.php.



PART 3: Checkout


8) Add code to shoppingCartPage.php to display another form (you should also leave the list of products in the cart there) to ask the user for delivery address and payment information. Add a “Confirm Purchase” button to the form. The action should still be “shoppingCartPage.php



9) Add to ShoppingCart class in shoppingCart.inc.php file a function that actually processes the purchase:


public static function buyCartProducts($db, $deliveryAddress, $payment)  


The function should write the sale data into the database, and if everything was OK, terminate the session (destroy the session variables and the session). If some error occurred, the session should not terminate. Write code to:

-Insert general order data into the Sale table in your database. Note that the SaleID is a surrogate key. The surrogate keys were created using AUTO_INCREMENT keyword, and the value for the surrogate key is generated by the database system. When writing the INSERT statement to insert into the table, you should not insert into the SaleID column (see the onlineTables.sql on the course calendar for an example). If you need the generated SaleID value for subsequent inserts, you should use $db->insert_id property on the database object you have ($db in the example). This property represents the ID created by the previous insert statement.

-Insert each product from the shopping cart in the SaleItem table. Note that the sale id should correspond to the sale id previously created (see above)

-If data was saved in the database, destroy the session. After session is destroyed, the customer should not see any items in the shopping cart, even if it executes the function again.

                        -Return true if everything ok and false otherwise


10) Add code to shoppingCartPage.php file to invoke the buyCartProducts with the posted values if the user clicks the “Confirm Purchase” button. Display appropriate message to confirm the order. If for any reason the data cannot be saved in the database, an error message should be displayed.


Here is a screen shot after entering address, credit card, and confirming the purchase in shoppingCartPage.php:



PART4 (Extra Credit) Update Cart


11) Modify your listCartProducts method to have the quantity in an input field of type text, so the user can modify the quantity. A good name for the input field is the barcode of the product.


12) Modify shoppingCartPage.php to display the list of products in cart in a form, and add an “Update Cart” button to the form. The action for the form should be “shoppingCartPage.php


13) Add code to shoppingCartPage.php to update the cart if the customer clicks on “Update Cart” button. For each product from the cart (from the session variable $_SESSSION[‘cart’]), check whether the post variable corresponding to the quantity for that product is greater than 0. If yes, update the quantity in the session variable, if not, delete that product from the session variable (unset ($_SESSION[‘cart’][$barCode]); should work fine, if $barCode is the barcode of the product). Re-display the shoppingCartPage.php


Test that new quantities are displayed in the shopping cart.


Turn in:

Electronic(due before 2359 on March 28, 2013)::

  1. Upload all files from your Lab10 folder to the Lab 10 assignment on the blackboard. 

Hard-copies (due before start of lab on March 29, 2013):

  1. The completed assignment coversheet. Your comments will help us improve the course.
  2. A print-screen of the web browser after last step (checkout) is completed.
  3. A hard copy of product.inc.php, productPage.php, shoppingCart.inc.php, shoppingCartPage.php