Before we begin developing the code for our custom shopping cart we first need to construct the database that will hold all of the various information required for a cart to function. In total there are 4 database tables which need to be created, each of which has a specific purpose within the process of running a shopping cart.
The first table we will create is the ‘categories’ table, this will hold the data for all of our product categories allowing us to organise the products more effectively. The table consists of three columns, our unique identifier called ‘categories_id’, the ‘parent_id’ which allows us to create unlimited sub categories and the ‘category’ column which stores the name of the category itself.
We will now construct the table which stores the product information.
The table is pretty standard, we have ‘products_id’ as our unique identifier and then the ‘categories_id’ which determines the category which each product is assigned to. We then have a ‘product’ column which will store the name of the item, moving onto the description which we will store in a text datatype so we can use HTML in our item description. Finally we have the price which is self explanatory and the weight which we will use to determine shipping costs of the product.
Next we move onto the ‘orders’ table:
This is quite clearly the largest of the tables and contains all of the customers information for billing and shipping, plus some details of the order itself such as the order values, it’s status, the methods used etc.
And finally, we create the ‘orders_products’ table:
This table consists of a unique column, then the ‘order_id’ column which tells us what order the product is associated with. We then store the product details in the remaining three columns. The reason we store this information rather than simply using a reference column such as ‘product_id’ is so that we can keep a historical view of the orders.
If we were to use a reference column each time you changed the price or the weight you would no longer have a history of previous orders.
So, that is all we need to begin the construction of the shopping cart. We may add tables or insert additional columns into the above for other purposes such as tax, fixed price shipping verses live shipping quotes etc, however these will be added on as modules rather than being part of the core system.
In the next chapter we will get started on the administration system and begin putting the file structure together for the project.