Tuesday, August 11, 2009

php shopping cart

Introduction

If you take a look around any PHP resource site, you'll notice an exorbitant amount of shopping cart scripts. The fact of the matter is that shopping cart scripts aren't hard to develop. A shopping cart script simply needs to work with some sort of storage device to store a list of items and how many of those items your visitor has chosen to add to their cart.

Typically we would use a database (such as MySQL) to store a list of products and their prices. We would then use PHP's session handling capabilities to store a list of products and amounts relating to each particular visitor, based on their session details.

This is a good way to do things, but what happens if you want to persist the details of that users cart across multiple sessions? I.e., if they close the web browser and come back the next day. You could setup a login system where users register and confirm their details by replying to a confirmation email, but more often you'd like to keep your checkout as simple as possible by just letting the user enter their address and payment method and continue right through to the confirmation page.

By persisting a cookie on the users machine and by using some PHP, we can make a simple shopping cart that will store the details of what the user wants to buy. These details will remain intact, even after the user closes the browser.

So, in this article we will build a simple persistent shopping cart. We will be using PHP and MySQL to do so, so you should have intermediate knowledge of both PHP and MySQL. You will be able to take the code that we will use and adapt it to create your own simple shopping cart for your web site.Building A Persistent Shopping Cart With PHP and MySQL - Creating the database

(Page 2 of 6 )

Let's assume that we're running a web site that sells Sony Playstation 2 games. We need one table to store the details of each product, and we also need a table to store the contents of each users shopping cart, so that they can be persisted over multiple sessions.

Fire up the MySQL console application and create a database named cart. Fill it with two tables: items and cart, like this:

create database cart;

create table items
(
itemId int auto_increment not null,
itemName varchar(50),
itemDesc varchar(250),
itemPrice decimal(4,2),
primary key(itemId),
unique id(itemId)
);

create table cart
(
cartId int auto_increment not null,
cookieId varchar(50),
itemId int,
qty int,
primary key(cartId),
unique id(cartId)
);

The first table, items, will contain a list of items that the user will be able to add to his/her cart. The items table contains four fields, as described below:

* itemId: A unique numeric identifier that gives each item its own ID.
* itemName: The name of the item in the catalog.
* itemDesc: A short description of the item in the catalog.
* itemPrice: The price of the item, such as 45.99.

The cart table will store the details of each item in the users cart as he/she adds them. The cart table also contains four fields, which are described below:

* cartId: A unique numeric identifier that gives each item in the users cart its own ID.
* cookieId: This is the most important field in both of the tables. It's used to persist the users cart over multiple sessions. It is the value of the session ID with which the user first started browsing our product range.
* itemId: The ID of the item that the user is purchasing.
* qty: The number of this specific item being purchased.

In this article I won't show you how to create forms to add, edit and delete items from the database. That's out of the scope of this article. We will manually add some products (Playstation 2 games) to the items table now:

insert into items values(0, 'Tony Hawk 3', 'Tony Hawk is back. Join him in this popular skating game where speed, collisions and tricks come together to produce the best skating game of all time!', 23.95);

insert into items values(0, 'FIFA Soccer 2002', 'The FIFA range of soccer games are the most popular in their genre. FIFA Soccer 2002 includes an all new team line up, advanced management capabilities, and richer, more realistic graphics.',36.50);

insert into items values(0, 'SSX Tricky', 'Image snowboarding down a steep hill at 100 miles per hour and you have SSX Tricky. It\'s packed with new players, new moves, and a whole new list of stages to complete.', 45.50);

Now that our database is good and ready to go, let’s create a simple PHP script that will list each item from the items table, providing a link to add each item to our shopping cart (which we will create later).

No comments: