If your Magento site supports reviews and ratings for products, as many do, then adding a way to sort by product rating is a feature that your customers may appreciate. The following is a quick way to add in "Rating" as a valid sort selection in category toolbars.
First we shall alter the way products are retrieved from the database, to include the overall rating (shown as the number of stars on the product) along with the rest of the product attributes. Copy the file
app/code/local/Mage/Catalog/Block/Product/List.php and open it for editing.
In the new
List.php file find the following line (around line 86):
$this->_productCollection = $layer->getProductCollection();
After this add the following:
$this->_productCollection->joinField('rating_summary', 'review_entity_summary', 'rating_summary', 'entity_pk_value=entity_id', array('entity_type'=>1, 'store_id'=> Mage::app()->getStore()->getId()), 'left');
Now we need to add in an option so that the customer can select "Rating" as an attribute to sort by. Copy the file
app/code/local/Mage/Catalog/Model/Config.php and edit.
In the new
Config.php file find the following code (which should start around line 298):
$options = array( 'position' => Mage::helper('catalog')->__('Position') );
Replace with code with:
$options = array( 'position' => Mage::helper('catalog')->__('Position'), 'rating_summary' => Mage::helper('catalog')->__('Rating') );
Now when viewing categories on your website you should have an option of "Rating" in addition to the others. Note that the sort order defaults to ascending so the lowest rated products will be displayed first. The sort order can be changed by the customer by clicking the arrow next to the drop-down box. Aside from this caveat the new sort is fairly easy to implement and extends the usefulness of the ratings.