Magento: How to Sort Products by Rating

sort product by rating

Magento provides default Sort options of Name, Price and Position in ‘Sort by’ drop down list on Product listing page. Often, store owners want to add additional sorting options to enable shoppers to browse their desired product (s) more quickly. Sometimes there is a need to add a sorting option of our choice. For example, if they want to sort products by rating. This is not supported by Magento so it is done through custom coding. Following is the way to add ‘Rating’ as a sort selection option.

First, the way products are retrieved from the database should be altered, 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/core/Mage/Catalog/Block/Product/List.php to

app/code/local/Mage/Catalog/Block/Product/List.php and open to edit it.

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/core/Mage/Catalog/Model/Config.php to

app/code/local/Mage/Catalog/Model/Config.php and edit.

In the new Config.php file, find the following code (around line 298):

$options = array(

'position'  => Mage::helper('catalog')->__('Position')

);

Replace the code with:

$options = array(

'position'  => Mage::helper('catalog')->__('Position'),

'rating_summary' => Mage::helper('catalog')->__('Rating')

);

Now you will see ‘Rating’ as a sort selection option in addition to the others.

2 thoughts on “Magento: How to Sort Products by Rating

Leave a Reply

Your email address will not be published. Required fields are marked *