Flylib.com

Books Software

 
 
 

Chapter 19. Ordering and Shipping at the Online Winestore


Chapter 19. Ordering and Shipping at the Online Winestore

This chapter is the fourth of five that outline the complete winestore application. In this chapter, we complete our description of the shopping components of the winestore by explaining the ordering and shipping modules. The ordering module collects and validates credit card details and manages the conversion of the shopping cart discussed in Chapter 18 to an order. In this process, the module manages the most complex database interactions in the winestore and uses locking to address concurrency problems.

The scripts discussed in this chapter perform the following functions:


Collect credit card and delivery details

Collects a credit card number, expiration date, and optional delivery instructions. These are validated using the techniques discussed in Chapter 9.


Finalize orders

Converts a shopping cart to an order and manages the sale of wine from the inventory table. This illustrates complex database processing that requires the locking techniques discussed in Chapter 8.


Email receipts

Sends a confirmation email to the user using the PEAR Mail package and a template that illustrates how the templates described in Chapter 7 can be used for non-HTML applications. Code that uses the PHP mail( ) library function is also included, but is not enabled by default.


HTML order receipts

Completes the ordering process with an HTML receipt that avoids the reload problem discussed in Chapter 6.


19.1 Code Overview

The ordering and shipping processes are implemented in the following scripts: order/order-step1.php , order/order-step2.php , order/order-step3.php , order/order-step4.php, and order/receipt.php .

The scripts work with the orders , items , and inventory tables discussed in Chapter 18:

CREATE TABLE orders (

  cust_id int(5) NOT NULL,

  order_id int(5) NOT NULL,

  date timestamp(12),

  instructions varchar(128),

  creditcard char(16),

  expirydate char(5),

  PRIMARY KEY (cust_id,order_id)

) type=MyISAM;



CREATE TABLE items (

  cust_id int(5) NOT NULL,

  order_id int(5) NOT NULL,

  item_id int(3) NOT NULL,

  wine_id int(4) NOT NULL,

  qty int(3),

  price float(5,2),

  date timestamp(12),

  PRIMARY KEY (cust_id,order_id,item_id)

) type=MyISAM;



CREATE TABLE inventory (

  wine_id int(5) NOT NULL,

  inventory_id int(3) NOT NULL,

  on_hand int(5) NOT NULL,

  cost decimal(5,2) NOT NULL,

  date_added date,

  PRIMARY KEY (wine_id,inventory_id)

) type=MyISAM;

The orders table includes the attributes instructions , creditcard , and expirydate that were omitted from our discussions in Chapter 18. These are used to store delivery and credit card details for each order that's shipped to a customer.

The script order/order-step1.php presents a form to the user that collects credit card and delivery details. The script is implemented using the same approach as the customer membership module in Chapter 17, and is based on the winestoreFormTemplate class discussed in detail in Chapter 16. Figure 19-1 shows the page rendered in a Mozilla browser.

Figure 19-1. The form that collects credit card and delivery details
figs/wda2_1901.gif

The credit card details are validated using the script order/order-step2.php . This script makes use of the validation functions in the include file validate.inc that's discussed in Chapter 16. On validation failure, the script redirects back order/order-step1.php , which redisplays the erroneous data and error messages. On validation success, the script continues to the script order/order-step3.php . This process is discussed later in "Credit Card and Shipping Instructions."

Step three of the process, which is implemented in the script order/order-step3.php, performs the database processing that converts a shopping cart to an order that's owned by a customer. The script updates the customer , order , and inventory tables, and checks for several error conditions that can occur, such as there being insufficient inventory to fulfill the order; details of this process are discussed later in Section 19.3. On error, the script redirects back to cart/showcart.php script discussed in Chapter 18, and on success it redirects to order/order-step4.php . The script itself produces no output.

After the order has been finalized, two receipts are produced. The first is an email receipt output by the script order/order-step4.php . The email is produced using the same template approach used to prepare a HTML page, and sent with the PEAR Mail package; we also include alternative code that uses the PHP mail( ) library function. The script then redirects to the final script in this chapter, order/receipt.php , that shows the user their receipt as an HTML page as shown in Figure 19-2.

Figure 19-2. An HTML order receipt
figs/wda2_1902.gif