6 min read

Providing wish lists

Wish lists allow customers to maintain a list of products that they would like to purchase at some point, or that they would like others to purchase for them as a gift.

Creating the structure

To effectively maintain wish lists for customers, we need to keep a record of:

  • The product the customer desires
  • The quantity of the product
  • If they are a logged-in customer, their user ID
  • If they are not a logged-in customer, some way to identify their wish-list products for the duration of their visit to the site
  • The date they added the products to their wish list
  • The priority of the product in their wish lists; that is, if they really want the product, or if it is something they wouldn’t mind having

Let’s translate that into a suitable database table that our framework can interact with:

Field

Type

Description

ID

Integer (Primary Key, Auto Increment)

A reference for the database

Product

Integer

The product the user wishes to purchase

Quantity

Integer

The number of them the user would like

Date added

Datetime

The date they added the product to their wish list

Priority

Integer

Relative to other products in their wish list, and how important is this one

Session ID

Varcharr

The user’s session id(so they don’t need to be logged in)

IP Address

Varchar

The user’s IP address (so they don’t need to be logged in)

By combining the session ID and IP address of the customer, along with the timestamp of when they added the product to their wish list, we can maintain a record of their wish list for the duration of their visit. Of course, they would need to register, or log in, before leaving the site, for their wish list to be permanently saved. This also introduces an element of maintenance to this feature, as once a customer who has not logged in closes their session, their wish-list data cannot be retrieved, so we would need to implement some garbage collection functions to prune this table.

The following SQL represents this table:

CREATE TABLE `wish_list_products` (
`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`product` INT NOT NULL,
`quantity` INT NOT NULL,
`user` INT NOT NULL,
`dateadded` TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP,
`priority` INT NOT NULL,
`sessionID` VARCHAR( 50 ) NOT NULL,
`IPAddress` VARCHAR( 50 ) NOT NULL,
INDEX ( `product` )
) ENGINE = INNODB COMMENT = 'Wish list products'
ALTER TABLE `wish_list_products` ADD FOREIGN KEY ( `product` )
REFERENCES `book4`.`content` (`ID`)
ON DELETE CASCADE ON UPDATE CASCADE;

Saving wishes

Now that we have a structure in place for storing wish-list products, we need to have a process available to save them into the database. This involves a link or button placed on the product view, and either some modifications to our product controller, or a wish-list controller, to save the wish. As wish lists will have their own controller and model for viewing and managing the lists, we may as well add the functionality into the wish-list controller.

So we will need:

  • a controller
  • a link in our product view

Wish-list controller

The controller needs to detect if the user is logged in or not; if they are, then it should add products to the user’s wish list; otherwise, it should be added to a session-based wish list, which lasts for the duration of the user’s session.

The controller also needs to detect if the product is valid; we can do this by linking it up with the products model, and if it isn’t a valid product, the customer should be informed of this. Let’s look through a potential addProduct() method for our wish-list controller.

/**
* Add a product to a user's wish list
* @param String $productPath the product path
* @return void
*/

We first check if the product is valid, by creating a new product model object, which informs us if the product is valid.

private function addProduct( $productPath )
{
// check product path is a valid and active product
$pathToRemove = 'wishlist/add/';
$productPath = str_replace( $pathToRemove, '',
$this->registry->getURLPath() );
require_once( FRAMEWORK_PATH . 'models/products/model.php');
$this->product = new Product( $this->registry, $productPath );
if( $this->product->isValid()
{
// check if user is logged in or not
if( $this->registry->getObject('authenticate')->
loggedIn() == true )
{
//Assuming the user is logged in, we can also store their ID,
// so the insert data is slightly different. Here we insert the
// wish into the database.
$wish = array();
$pdata = $this->product->getData();
$wish['product'] = $pdata['ID'];
$wish['quantity'] = 1;
$wish['user'] = $this->registry->getObject('authenticate')->
getUserID();
$this->registry->getObject('db')->
insertRecords('wish_list_products', $wish );
// inform the user
$this->registry->getObject('template')->getPage()->
addTag('message_heading', 'Product added to your wish list');
$this->registry->getObject('template')->getPage()->
addTag('message_heading', 'A ' . $pdata['name']
.' has been added to your wish list');
$this->registry->getObject('template')->
buildFromTemplates('header.tpl.php', 'message.tpl.php',
'footer.tpl.php');
}

The customer isn’t logged into the website, so we add the wish to the database, using session and IP address data to tie the wish to the customer.

else
{
// insert the wish
$wish = array();
$wish['sessionID'] = session_id();
$wish['user'] = 0;
$wish['IPAddress'] = $_SERVER['REMOTE_ADDR'];
$pdata = $this->product->getData();
$wish['product'] = $pdata['ID'];
$wish['quantity'] = 1;
$this->registry->getObject('db')->
insertRecords('wish_list_products', $wish );
// inform the user
$this->registry->getObject('template')->getPage()->
addTag('message_heading',
'Product added to your wish list');
$this->registry->getObject('template')->getPage()->
addTag('message_heading', 'A ' . $pdata['name']
.' has been added to your wish list');
$this->registry->getObject('template')->
buildFromTemplates('header.tpl.php', 'message.tpl.php',
'footer.tpl.php');
}
}

The product wasn’t valid, so we can’t insert the wish, so we need to inform the customer of this.

else
{
// we can't insert the wish, so inform the user
$this->registry->getObject('template')->getPage()->
addTag('message_heading', 'Invalid product');
$this->registry->getObject('template')->getPage()->
addTag('message_heading', 'Unfortunately, the product you
tried to add to your wish list was invalid, and was not
added, please try again');
$this->registry->getObject('template')->
buildFromTemplates('header.tpl.php', 'message.tpl.php',
'footer.tpl.php');
}
}

Add to wish list

To actually add a product to our wish list, we need a simple link within our products view. This should be /wishlist/add/product-path.

<p>
<a href="wishlist/add/{product_path}"
title="Add {product_name} to your wishlist">
Add to wishlist.
</a>
</p>

We could encase this link around a nice image if we wanted, making it more user friendly. When the user clicks on this link, the product will be added to their wish list and they will be informed of that.

LEAVE A REPLY

Please enter your comment!
Please enter your name here