Displaying filtered productsAssuming we call our filterProducts method within the products controller at some point, we can filter our products list quite easily.. In our "products list"
Trang 1$somethingToFilter = true;
// build the query
$sql = " AND ";
$assocs = implode( " AND ", $this->filterDirect );
$sql = $assocs;
}
if( $somethingToFilter )
{
// since we have some filter requests, store the query.
$this->filterSQL = $sql;
}
}
And secondly, we look at a function to build our SQL statement
/**
* Add SQL chunks to our filter arrays, to help build our query,
* based on actual filter requests in the URL
* @param String $filterType the reference of the attribute type we
* are filtering by
* @param int $filterValue the ID of the attribute value
* @return void
*/
private function addToFilter( $filterType, $filterValue )
{
if( $this->filterTypes[ $filterType ]
['ProductContainedAttribute'] == 1 )
{
$lower = $this->filterValues[ $filterValue ]['lowerValue'];
$upper = $this->filterValues[ $filterValue ]['upperValue'];
$sql = " p.{$filterType} >= {$lower}
AND p.{$filterType} < {$upper}";
$this->filterDirect[] = $sql;
}
else
{
$this->filterCount++;
$sql = " pfaa.attribute={$filterValue} ";
$this->filterAssociations[] = $sql;
}
}
Trang 2Displaying filtered products
Assuming we call our filterProducts() method within the products controller
at some point, we can filter our products list quite easily In our "products list" page,
for instance, we can simply detect if the filter SQL field is empty; if it is not, we
can replace the list query with the filter query Of course, we should also swap
our template, to indicate that the results are a filtered subset
private function listProducts()
{
if( $this->filterSQL == '' )
{
$sql = "SELECT p.price as product_price,
v.name as product_name,
c.path as product_path
FROM content c, content_versions v,
content_types_products p
WHERE p.content_version=v.ID AND v.ID=c.current_revision
AND c.active=1 ";
}
else
{
$sql = $this->filterSQL;
}
$cache = $this->registry->getObject('db')->cacheQuery( $sql );
$this->registry->getObject('template')->getPage()->
addTag( 'products', array( 'SQL', $cache ) );
$this->registry->getObject('template')->
buildFromTemplates('header.tpl.php',
'list-products.tpl.php',
'footer.tpl.php');
$this->generateFilterOptions();
}
Remember, we must first call our filterProducts method, so I've added this to the
switch statement within the controller's constructor
$urlBits = $this->registry->getURLBits();
$this->filterProducts( $urlBits );
Trang 3If we have filters in place with respect to price and weight (which are based off the
products table), they would look like this:
If we click on one of the options, the products list would update to show products
matching that criterion
Improving product filtering
As with everything, there is always room for improvement For the filter feature,
potential improvements include:
Displaying the number of products matching a filter next to it
Pagination—limiting the number of products displayed to the initial Y
results, allowing the customer to move to the next set of results, so they are
not overwhelmed with products
Updating this number to account for filters already in place (that is, if there
are 100 brand X products and we filter the price to < $5, there may only be 20
matching brand X products, and the number should update to reflect this)
Filter options with no matching products could be hidden, to prevent the
customer from clicking them, and finding that it made no change
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
•
•
•
•
Trang 4Creating 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:
Auto Increment) A reference for the database
to purchase
would like
to their wish list
in their wish list, and how important is this one
don't need to be logged in)
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
•
•
•
•
•
•
Trang 5The 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
•
•