Welcome to building a PHP e-commerce framework! During the course of this book we are going to build a flexible e-commerce framework using PHP, which can be extended and modified for the purposes of any e-commerce site.
In this chapter, you will learn:
The business logic behind e-commerce
Why (and when) you should use your own system over an existing product
The benefits of a "framework"
About existing e-commerce sites and products
e-commerce, or electronic commerce, is the sale and purchase of goods or services through electronic means. In our case, this electronic means is the Internet. There are so many different applications of e-commerce on the Internet, including:
e-commerce is an incredibly popular way of doing business, so let's look at who is using e-commerce and what they are using it for.
According to eBay's website, there are approximately 84 million active users of eBay, with users trading more than $1,900 worth of goods each second. That means 84 million of us are using eBay to buy and sell goods, either as a business sustaining a regular turnover, or to try and make a little extra cash by selling unwanted or unneeded things knocking about the house.
eBay is a social e-commerce site, operating as an online auction house, whereby they don't actually sell anything themselves, but instead allow their community of users to not only buy but also sell through their site. This not only illustrates the popularity of e-commerce, but also that there is money to be made in providing a stage for low (and high) volume online purchases.
With revenue of over $19 billion in 2008, Amazon is one of the most popular e-commerce sites on the Internet. Research in early 2009 indicated that it was the favorite retailer for both video and music in the UK.
Large, established Brick 'N Mortar stores such as Wal-Mart, Tesco, and Borders use online shops to sell the products they generally keep in store. With the likes of Wal-Mart and Tesco, customers often book a delivery timeslot for their groceries to be delivered. They also offer more than what is available in store, which they can easily bolt on for the convenience of their customers. With online retail, sellers are not confined to what they can stock on the shelves, but what they can store in their distribution warehouses.
Smaller, niche-based Brick 'N Mortar stores use online selling as a way to target their products to a wider audience, without the limitation of their existing physical presence.
Companies such as 37signals are setting up online applications (such as Project Management tool, BaseCamp) with monthly subscription models. Other examples of such sites include large file distribution websites (allowing you to "e-mail" large files using a third-party website) and premium features on certain websites, for example Get Satisfaction.
The popularity of online shopping has increased dramatically over the past few years. Not only does it provide the convenience of allowing customers to shop in the comfort of their own home, it also allows businesses to trade on a global marketplace, targeting even more potential customers. Because everything is done electronically, e-commerce stores can also help generate recurring revenue, by recommending new products to customers based on previous purchases, and by keeping them up to date with the store's catalog.
Throughout the course of this book, we are going to build a framework of our own, using PHP, as opposed to making use of an existing product. Sometimes, it is more appropriate to use existing solutions; sometimes it is better to use your own solutions. As you are reading this book, hopefully you know why you want to create your own framework. However, let's look at why we are going to create ours.
PHP is a very popular language, and because it isn't a framework in its own right, we can easily structure our framework out of it, however we wish. The main choice for a programming language is generally down to your own preference.
Most modern web hosts support PHP and MySQL, and while languages like Ruby on Rails are gaining popularity, at the moment hosting for them is not as common. This book assumes that you already have a reasonable understanding of PHP, so hopefully that will also be an important factor in why you want to use PHP; perhaps you need to develop something quickly, and don't want to use a language or platform that is out of your comfort zone.
Instead of looking to create an e-commerce system, designed to perform all types of e-commerce tasks, we will create a framework. This will make it easy to extend the needs of any e-commerce project with minimal effort. Because we are creating our own framework, it is going to be something we will know and understand very well, meaning that if we do need to extend it or use it, we can do so easily.
While a typical e-commerce system may show products within a browse or search interface, a framework could allow us to integrate products into other areas of a website; for instance, pulling certain categories of product into relevant pages, particularly useful for a website that needs to do more than just sell online. For example, if we were selling books, we could have pages dedicated to certain authors with information, reviews, and other media about the author, and then integrate some of their popular products into the page.
There are already a number of e-commerce systems available, written in various different languages, and sometimes it is more appropriate to use such a product, for example:
When you have a tight deadline for a project, and you don't have a framework in place
When there are lots of developers on the project; something with more documentation available would probably be more useful, at least initially (unless the framework was developed by most of the developers)
When the features match — if another system has all the features you need and want, and it works in a way you are comfortable with, then it would be more appropriate to use the existing system
Of the e-commerce applications that are already available, the following are amongst the most popular:
Magento: This is a very modular and flexible e-commerce platform, which is becoming more widespread in its usage.
Drupal e-commerce: Drupal is a popular content management system, which is easy to extend and modify. There are two packages of modules, Ubercart and Drupal e-commerce, which add a wealth of e-commerce functionality to the popular CMS.
CubeCart: This is a simple-to-use e-commerce solution, available with both free and paid versions.
We have already taken a brief look at who is using e-commerce; let's take a more detailed look at some popular e-commerce sites, and see how they work and what features they provide to their users.
iStockphoto is a popular website for buying and selling stock photography. Photographers can register on the website and submit their photographs for approval. Approved photographs can then be purchased by customers for a number of credits, depending on the size of the photograph and the license they wish to purchase it under.
Approval process for sellers: Photographs are approved by iStockphoto before being available for purchase.
Flexibility for sellers: Sellers can choose their prices, multiple image sizes, and the licenses they wish to sell the image under.
Credits: Because most stock photograph purchases equate to only a few dollars, iStockphoto has a credits system whereby the customers purchase at least $10 of credits, which are assigned to their accounts. These credits are then deducted when they make a purchase.
Social: Photographs can be rated and commented on, making the website very social and interactive.
WooThemes is quickly becoming a popular online shop for custom themes, but operates quite differently to most theme-selling websites. Purchases are either a theme package (the theme and accompanying color schemes), or a one-off fee along with a monthly subscription allowing the customer to purchase any themes they wish in a particular month, backed up with reassurances of a minimum number of themes each month.
WooThemes also invites established members of the web design community to create themes for the site, helping to raise the profile of the site and continue their ability to offer quality themes.
We discussed earlier, that eBay is an online auction house, but what features does it have to support its business?
While Amazon doesn't operate as an online auction house, as eBay does, it still has a number of social aspects to it, including ratings, reviews, and recommendations. It also allows users of the site to sell their own copies of products listed within the store, through the Amazon Marketplace. This market place functionality is also integrated within their main product listing, informing customers that they can also purchase used and new copies of a product, from non-Amazon sellers, through its market place.
Play.com operates in a similar way to Amazon: it not only sells products, but also allows users to sell their own items (branded as PlayTrade). One notable difference with Play.com is the categorization of products, which also allows more dynamic categories such as products under a certain amount, or seasonal items (for example, Christmas present ideas).
After looking at some popular e-commerce sites, our store obviously needs some key features. It needs the ability for users to search for and browse products, within different categories. Visitors to the site obviously need to be able to purchase these products, which leads to the need for a shopping basket to store products the visitor intends to purchase and a checkout process to manage delivery details, tax calculations, delivery charge calculations, payment processing, and of course order management for administrators. We will build upon these core features to build a basic feature list for our framework.
The exception with regards to those features is eBay, which forgoes the need for a shopping basket; however, it contains provisions for watching items, automatically bidding for items, and with "Buy it now" making an instant purchase.
We need to have the following product-related features:
Finding products: We need the functionality to both browse product listings and search for products to make it easy for customers to find products within our framework.
Viewing products: Once customers have found a product that interests them, they obviously need to be able to view the product to find out more about it. This also means we need to take into consideration what type of information is related to products (for example name, price, weight, and so on). Community-orientated aspects link in nicely here too, such as ratings and reviews.
Expressing interest in a product: This can be done either by adding a product to a basket, or to an interest list, for future purchase.
The checkout process essentially has the following three features or requirements:
We are going to create a framework that can do anything we need it to. Of course, the exact needs of a project vary from project to project, so we will ensure it has some fundamental features, which we can then extend to whatever we need. The following features will be the minimum that we will have our framework capable of doing:
Displaying and managing products
Displaying and managing categories of products
Embedding products, listings, and categories into other aspects of a website or web application (after all, it is a framework we are creating!)
Customizing products such as apparel
Searching for products
Filtering the product list based on the customers' preferences, such as brand, or other properties
Providing wish lists, that is, lists of products that users wish to purchase at some point, or would like someone to purchase for them (including the provision to facilitate gift purchases)
Generating recommendations based on previous purchases
Sending e-mail notifications when certain products are in stock
Publishing ratings and reviews of products
Providing a shopping basket to store products and quantities of the products a customer wishes to purchase
Calculating shipping cost
Based on products and/or their weights
Based on delivery address
Based on custom rules (for example free shipping to orders over a fixed amount)
Tax cost calculations
Managing discount codes
Managing gift certificates
Processing payments
Allowing customers to manage their account
Allowing us and other administrators to manage the store
Along with these features, we are also going to look at the following functionalities:
Deploying the framework into a live store environment
Backing up and restoring the store
Enabling secure connections to the live store using SSL
To illustrate how our framework can be extended to meet the needs of any e-commerce situation, there are three appendices looking at different ways to extend the framework:
Web service integration, for services such as Google Product Search
Extending our store to support downloadable products
Various code snippets in a cook book format, showing how to quickly extend this (and any other framework) to support some specific enhancements
Most online stores are there for a particular purpose, either to sell a particular product, or to act as the online division of a Brick 'N Mortar store. Obviously, the point of creating a framework is to easily adapt to any purpose; however, for the purposes of this book, we are going to need a scenario site to create.
Juniper Theatricals is a fictional Brick 'N Mortar novelty and theatrical supplies store based in Newcastle upon Tyne, UK. They have some customers who place orders over the telephone, or over fax, and have a loyal local base of customers too. When building our framework, we are going to use it as a web presence for this fictional store. Some specific requirements of the store include, in addition to previously discussed features:
Customizable products: Apparel.
Custom products: User-defined images and text on apparel, perhaps the ability to list these for sale as well.
Virtual purchases: Electronic tickets to events.
Because the store has no web presence, the framework needs to work for their entire website, integrating e-commerce functionality where appropriate. Although they are based in the UK, the website is designed to target new customers, primarily in the USA, so the site will use USD as its primary currency.
Let's look at what our framework will look like for our Juniper Theatricals store. Here's an insight to a product view powered by our framework:
In this chapter we looked at e-commerce and discussed the reasons for creating our own e-commerce framework in PHP, along with the features our framework is going to support. We also looked at some existing e-commerce setups and discussed the different types of e-commerce stores available on the Internet. Now that we know what we are going to create and why, we are ready to start building the structure and basic functionality of our framework, before adding a wealth of e-commerce functionality.