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:


  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;


  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

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