Now that we understand how we should build our product catalog, let's shift our attention to the shopping cart. What exactly is a shopping cart, and how should it be built? A shopping cart is simply a list of products that the customer wants to buy, so it turns out to be really easy to build. In fact, our shopping cart will just be an associative array of product ID's and quantities, and it will be stored as a session variable ($SESSION["cart"]). The basic operations that we'd like to be able to do with our shopping cart is:
Our shopping cart will be an object (of the Cart class). If you are new to object oriented programming in PHP, this will be a good introduction for you. Our cart will be an object with two properties:
Adding Products to the Cart Adding a product to the cart is as easy as putting the product ID into the $items array. We just increment the quantity of that product ID in our array. Remove Products from Cart Even easier than adding a product to the cart, we just have to remove this product ID from the $items array by unsetting it.
Counting the Products in the Cart We can't just return the number of records in our array, because that will only tell us the number of distinct products in the shopping cart. We have to iterate through our $items array and count up the quantities of each product .
Calculating the Price of the Cart Items This is the toughest part of the shopping cart, we have to add up the price of all the items in our shopping cart. You will notice that we don't keep track of the price of the products in our shopping cart, we only keep the product ID and the quantity of that product. What we've got to do is ask the database how much each item costs, multiply that with the quantity ordered to get a total, and then sum up all the totals. The general algorithm would be like this:
That's the general algorithm, but it is slow if the customer had lots of products in his cart. Reason is that we made one separate SELECT query for each product, this is bad. Instead, what we will do is construct a list of product ID's that we are interested in and only executing just one query. So our improved algorithm is:
The algorithm got a little bit more complex, but that saved us from running lots of individual queries (which would kill your database when your millions of customers rush to buy things at the same time, hehe).
Viewing the Shopping Cart So far we have dealt with adding, removing, and counting things up in the shopping cart. We will now look at viewing and updating the contents of the shopping cart. When the customer goes to view his/her shopping cart, we want to let them change the quantities of the products they ordered. We will also let them empty out their shopping cart. These operations are straight forward enough that we won't explain them, the source code should be self-explanatory. From here we let the customer purchase the products in the shopping cart. Up to this point, we have not required the customer to log in. IMPORTANT NOTE: It is probably not a good idea to make your customers log in before you let them browse your product catalog or play with the shopping cart. It is only when the customer really wants to make a purchase that you should ask them to log in. Why? So you can pull up their personal information (name, address) from the database. Before this point you don't really care who they are (unless you keep some profile of them ...) Once you have they've logged on, you will ask them to verify their billing information and also to supply their credit card information and any special instructions or comments for this purchase. IMPORTANT NOTE: You should not store credit card information in the database, instead it is safer to ask for it only when the customer is actually going to purchase something. I'd be pretty scared and upset if I walked into a department store and they had my credit card information on file! Okay, so the customer enters in his information, the next step is to show a confirmation screen telling them what is going to happen. Basically a summary of the order and the billing details / instructions that the customer has entered in so far. If all is well, the customer will confirm the order. What comes next is payment processing and order fulfillment. These will be the topics for our next sections.
blog comments powered by Disqus |
|
|
|
|
|
|
|