The Shopping Cart

Once you have created a product catalog, as I have now, the actual shopping cart itself can be surprisingly simple. The method I’ve chosen to use in this example is to record the product IDs, prices, and quantities in a session. Knowing these three things will allow the scripts to calculate totals and do everything else required.

These next two examples will provide all the necessary functionality for the shopping cart. The first script, add_cart.php, will add items to the shopping cart. The second, view_cart.php, will both display the contents of the cart and allow the customer to update it.

Adding items

The add_cart.php script will take one argument—the ID of the print being purchased—and will use this to update the cart. ...

Get PHP and MySQL for Dynamic Web Sites: Visual Quickpro Guide, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.