How to Add Sort by Rating in Magento

ce-1.9.1.0ratingssorting

I want to add a sort by rating on product list view. Product rating is not an attribute, so I can't edit it in the magento backend.

So I think the best way is to create a module for this problem, but I don't know how. Hope you can help me?

EDIT:

the snippet below from amasty works like the solution from http://www.fontis.com.au/blog/magento/sort-products-rating but both gererate the following error only on homepage:

Joined field with this alias is already declared with the following stacktrace:

0 /web/magento/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(695): Mage::exception('Mage_Eav', 'Zusammengef??hr...')
#1 /web/magento/app/code/local/Mage/Catalog/Block/Product/List.php(98): Mage_Eav_Model_Entity_Collection_Abstract->joinField('test', 'review/review_a...', 'rating_summary', 'entity_pk_value...', Array, 'left')
#2 /web/magento/app/code/local/Mage/Catalog/Block/Product/List.php(154): Mage_Catalog_Block_Product_List->_getProductCollection()
#3 /web/magento/app/code/core/Mage/Core/Block/Abstract.php(918): Mage_Catalog_Block_Product_List->_beforeToHtml()
#4 /web/magento/app/design/frontend/nextlevel/nextlevel-german/template/page/html/midcolumn.phtml(4): Mage_Core_Block_Abstract->toHtml()
#5 /web/magento/app/code/core/Mage/Core/Block/Template.php(241): include('/web/magento...')
#6 /web/magento/app/code/core/Mage/Core/Block/Template.php(272): Mage_Core_Block_Template->fetchView('frontend/nextle...')
#7 /web/magento/app/code/core/Mage/Core/Block/Template.php(286): Mage_Core_Block_Template->renderView()
#8 /web/magento/app/code/core/Mage/Core/Block/Abstract.php(919): Mage_Core_Block_Template->_toHtml()
#9 /web/magento/app/code/core/Mage/Core/Block/Text/List.php(43): Mage_Core_Block_Abstract->toHtml()
#10 /web/magento/app/code/core/Mage/Core/Block/Abstract.php(919): Mage_Core_Block_Text_List->_toHtml()
#11 /web/magento/app/code/core/Mage/Core/Block/Abstract.php(637): Mage_Core_Block_Abstract->toHtml()
#12 /web/magento/app/code/core/Mage/Core/Block/Abstract.php(581): Mage_Core_Block_Abstract->_getChildHtml('midcolumn', true)
#13 /web/magento/app/design/frontend/nextlevel/nextlevel-german/template/page/1column.phtml(40): Mage_Core_Block_Abstract->getChildHtml('midcolumn')
#14 /web/magento/app/code/core/Mage/Core/Block/Template.php(241): include('/web/magento...')
#15 /web/magento/app/code/core/Mage/Core/Block/Template.php(272): Mage_Core_Block_Template->fetchView('frontend/nextle...')
#16 /web/magento/app/code/core/Mage/Core/Block/Template.php(286): Mage_Core_Block_Template->renderView()
#17 /web/magento/app/code/core/Mage/Core/Block/Abstract.php(919): Mage_Core_Block_Template->_toHtml()
#18 /web/magento/app/code/core/Mage/Core/Model/Layout.php(555): Mage_Core_Block_Abstract->toHtml()
#19 /web/magento/app/code/core/Mage/Core/Controller/Varien/Action.php(390): Mage_Core_Model_Layout->getOutput()
#20 /web/magento/app/code/core/Mage/Cms/Helper/Page.php(137): Mage_Core_Controller_Varien_Action->renderLayout()
#21 /web/magento/app/code/core/Mage/Cms/Helper/Page.php(52): Mage_Cms_Helper_Page->_renderPage(Object(Mage_Cms_IndexController), 'startseite')
#22 /web/magento/app/code/core/Mage/Cms/controllers/IndexController.php(45): Mage_Cms_Helper_Page->renderPage(Object(Mage_Cms_IndexController), 'startseite')
#23 /web/magento/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Cms_IndexController->indexAction()
#24 /web/magento/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('index')
#25 /web/magento/app/code/community/Mdl/UnderConstruction/Controller/Router/Standard.php(72): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#26 /web/magento/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mdl_UnderConstruction_Controller_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#27 /web/magento/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#28 /web/magento/app/Mage.php(684): Mage_Core_Model_App->run(Array)
#29 /web/magento/index.php(87): Mage::run('', 'store')
#30 {main}

It seems to be an error with the custom theme, but changing the alias not working for me, so I dont know where the problem is, hope u can help me:)

Best Answer

To sort products by rating you can use the following code, taken from this module:

$collection->joinField(
        'rating_summary',                // alias
        'review/review_aggregate',      // table
        'rating_summary',               // field
        'entity_pk_value=entity_id',    // bind
        array(
            'entity_type' => 1, 
            'store_id' => Mage::app()->getStore()->getId()
        ),                              // conditions
        'left'                          // join type
    );
    $collection->getSelect()->order('rating_summary desc');