This section outlines the functions of the scripts that implement the shopping cart and winestore home page. The cart is implemented in the cart/showcart.php, cart/addtocart.php, cart/emptycart.php, and cart/updatecart.php scripts. The home page is implemented as index.php. The database-based cart management process is discussed later in Section 18.3.
The script cart/showcart.php is the only shopping cart script that presents output to the user. It shows the cart's current contents using a form in an HTML table environment that allows the user to modify the quantity of any of the items. If the cart contains no items, a message is shown instead. Figure 18-1 shows an example of a cart containing four items.
If the user modifies any quantity in the cart, then he must click on the Update Quantities button to write the change to the database. This submits the form and requests the script cart/updatecart.php , which processes the variables and values passed with the GET method and updates the quantities. Setting a quantity to zero deletes the item. If the user wants to completely empty the cart, he can click on Empty Cart which requests the cart/emptycart.php script. cart/updatecart.php and cart/emptycart.php don't produce any HTML output, but instead redirect the browser back to cart/showcart.php.
The cart/addtocart.php scripts adds a quantity of a wine to the cart. It can be requested from both the home page and the search/search.php script discussed in Chapter 20. The script expects two parameters to be passed: the wine to be added (referenced by its wine_id) and the quantity to add. The script handles the three different cases that can occur when adding a wine item to the cart:
The item is the first to be added to an empty cart, and a new cart needs to be created before the item is added.
The item is a new item being added to an existing cart.
The item is already in the cart and the user is adding an additional quantity.
When a wine is added for the first time to the cart, the script also discovers the cheapest price of that wine in the inventory (as per the requirements in Chapter 16).
The homepage script index.php displays the three wines that have most-recently been reviewed by a wine writer. With each of these wines, it includes embedded links that can be clicked on to add one or a dozen bottles to the shopping cart using the cart/addtocart.php script. The home page also displays a welcome message, and buttons that allow the user to search the wines, become a member or amend member details, and login or logout. The home page is shown in Figure 18-2.