Home arrow PHP arrow Page 6 - Building an E-Commerce Site Part 3: Catalogs and Shopping Carts

The Shopping Cart - PHP

This is the third and final article in a three-part series dealing with using PHP 4 and MySQL to make a comprehensive e-commerce storefront solution. This article covers the shopping cart, payment processing, and database engine considerations, among many other topics. Full source code included!

  1. Building an E-Commerce Site Part 3: Catalogs and Shopping Carts
  2. Assumptions and Requirements
  3. Overview of The Process
  4. MyMarket Shopping Experience
  5. The Product Catalog
  6. The Shopping Cart
  7. Payment Processing
  8. Step 1: Database Changes
  9. Step 2: Extracting the New Scripts
  10. Step 3: General Script Changes from Tutorial 2
  11. Step 4: New Shopping Scripts
  12. Conclusion
By: Ying Zhang
Rating: starstarstarstarstar / 50
June 08, 2000

print this article



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:

  1. Add products to the cart
  2. Remove products from the cart
  3. Count up the number of products in the cart
  4. Calculate the total price of the products in the cart

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:

  1. $items
    The associative array of product ID's and quantities. The key is the product and the value is the quantity
  2. $total
    The total price of the items in the cart. We will only update this as necessary, because it is a bit of work to calculate.
  3. The associative array of product ID's and quantities. The key is the product and the value is the quantityThe total price of the items in the cart. We will only update this as necessary, because it is a bit of work to calculate.


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:

Cart_Total = 0 for each product (P) in the shopping cart SELECT price FROM products WHERE id = P Cart_Total = Cart_Total + price * quantity of P ordered loop

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:

Product_List = "" for each product (P) in the shopping cart Product_List = Product_list + P loop Cart_Total = 0 SELECT id, price FROM products WHERE id IN (Product_List) for each record in the query result Cart_Total = Cart_Total + price * quantity of id ordered loop

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.

>>> More PHP Articles          >>> More By Ying Zhang

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: