Continuing our series on setting up an online bookstore with Ruby-on-Rails, this two-part tutorial explains how to implement the shopping cart. It is excerpted from chapter five of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).
In the previous chapter, we built the general interface for Emporium. That’s a good start for an online store, but it doesn’t bring George any revenues. Now it’s time for us to take the next step toward a working e-commerce site: create a shopping cart where customers can drag all the interesting books they find—and preferably a few more.
Getting the Shopping Cart Requirements
Again, Jill, George’s faithful customer, plays the main role in our user stories for this sprint:
Add items to the cart: The most important shopping cart feature of any e-commerce application is the ability to add items to the cart. If building up a heaping shopping cart is easy, that will have a positive effect on the cash flow.
Remove items from the cart: No matter how badly George wants Jill to buy all the books she inadvertently added to the cart, that’s just not how you make customers happy.
Clear the cart: Sometimes Jill goes totally bonkers. She just can’t help adding every book she runs into to the shopping cart. But then she remembers she is on a pension, not on a 100-foot yacht, and just wants to clean up the darn thing. So, we need to make it possible to empty the shopping cart with a single stroke.
The last user story to implement for a shopping cart is check out—the ultimate goal of a web shopping tour. Checking out is a bit more complicated than the other user stories, so we’re going to tackle it separately in Chapter 9. There, we’ll also cover how to integrate with credit card payment gateways.