Abstract Cart

This was really fun project! The user should never know the difference, but the background stuff works better. It seems a lot of the things I like are never seen by the public.
When I was designing the shopping cart for tadaatiedye.com, using the database was the obvious answer. But I noticed a lot of sites allow a guest to save items to the cart. I wanted to do that, too! I could have added the items to the database with a placeholder userID. Then if they login, I'd add that userID to their profile or update it with their userID. I took a different approach with saving the items to SESSION variables, then they could check out as a guest.

I decided that I needed two kinds of shopping carts, one with SESSION varibles, one that used the database. I could have written them directly; I decided to use an abstract class because:

Since this was also my model class for the shopping cart, I used the abstract class Cart to define the fields or attributes and methods (actions) needed for every cart.

  • public $cart;
  • public $message;
  • public $cartTotal;
  • public $itemCount;
  • abstract function addtocart($itemID, $qty);
  • abstract function updatecart($itemID, $qty);
  • abstract function removefromcart($item);
  • abstract function emptycart();
  • abstract function gettotal();
  • abstract function getcart();
  • private function adjustQtyToInventory($itemID, $qty)

Since I needed all of the fields for the View, they needed to be public. I noticed that it really didn't matter what kind of cart you have to adjust the quantity ordered to available inventory. This is defined in the abstract class. In PHP, all methods of parent class are available to the derived classes.

With this framework in place, I could work with the UI/View and controller to see what I needed without worrying how it needed to be done. (This is the 'wild and free' part of abstract classes!)

Next, the derived classes (sessionCart and dbCart) had to figure out how to implement the functions defined in the ShoppingCart class.