Category Page Product Sorting

In this blog we will see basics of how to product sorting in category page and how to setup different sorting type.

We will also see how to set default sorting in product collection e.g if we want to sort products in ascending order by price,name etc.
By default the product sorting drop down in category page is show here Magento Admin Manage Category Display Settings Tab. Suppose we want to add/remove sorting options from the drop down go to the Admin -> Manage Categories and then edit the current category, there go to the “Display Settings” Tab. There will see the list of sorting attributes that show up in the category page drop down, and you can also see an option to select default sorting method.

Magento Admin Category Page Display Setting Tabs

Magento Admin Category Page Display Setting Tabs

If we want to add more sorting options, i.e we have some more attributes which we want to have option to sort by. For this go to Catalog -> Attributes -> Manage Attributes in admin. Open the edit page of the attribute which you want to add to sort. There in the “Fontend Properties” box you will get the option for “Used for Sorting in Product Listing”, make this drop down as Yes and then this attribute will show up on the Manage Category Page. Magento Admin Manage Attributes

Custom Product Sorting In Category Page

Support we want to add a custom product sorting option, like sort product by created_date or update_date. For this we need to do some coding.
Lets take a simple example first i.e need to sort product by created_date. First will add an option to our drop down.
For this open the file Mage_Catalog_Model_Config and we need to make changes to the function getAttributeUsedForSortByArray().

 public function getAttributeUsedForSortByArray()
        $options = array(
            'position'  => Mage::helper('catalog')->__('Position'),
            'created_at'  => Mage::helper('catalog')->__('Product Creation Date')
        foreach ($this->getAttributesUsedForSortBy() as $attribute) {
            /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
            $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();

        return $options;

You need to override this class to make the changes
Next we need to make changes to the our collection, so that products are sorted by our custom drop down value. But right now, since ‘created_at’ is already a product attribute, we don’t need to make changes anywhere else.
Let’s take another example where we will sort products by number of reviews. Since review is not a product attribute, we need to make changes to our product collection. Rest everything is same as far as adding option to drop down is concerned.
For this open file Mage_Catalog_Block_Product_List_Toolbar and around line 221 there is a function ‘setCollection’. You need to override this class in your module and then change the ‘setCollection’ function to

public function setCollection($collection)
		$this->_collection = $collection;


		// we need to set pagination only if passed value integer and more that 0
		$limit = (int)$this->getLimit();
		if ($limit) {
                if($this->getCurrentOrder() == 'review'){
        else if ($this->getCurrentOrder()) {
            $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
		return $this;

As you can see we have added a new function to the product collection sortByReview(), so we need to implement this function. For this we will override the product collection class and add the function sortByReview(). For tutorial on override view this blog article.
The sortByReview function is given below

public function sortByReview($dir){
    	$table = $this->getTable('review/review');
    	$entity_code_id = Mage::getModel('review/review')->getEntityIdByCode(Mage_Rating_Model_Rating::ENTITY_PRODUCT_CODE);
    	$cond = $this->getConnection()->quoteInto('t2.entity_pk_value = e.entity_id and ','').
    	$this->getConnection()->quoteInto('t2.entity_id = ? ',$entity_code_id);
    	$this->getSelect()->joinLeft(array('t2'=>$table), $cond,array('review' => new Zend_Db_Expr('count(review_id)')))
    	->group('e.entity_id')->order("review $dir");
  • Maha Khalaf

    thanks for this useful article.. I would like to implement sort by “most popular”. I know it should be similar to sort by reviews.. but how can I implement the function “sort_by_popular”?

  • Manish Prakash

    Sort by popular would also be same as Sort by review?

    i have written the sort by review function in the blog post already? or is there something else your asking?

  • Maha Khalaf

    yes I am asking if you can kindly help me implement “sort by popular” please

    • Manish Prakash

      what kind of help you require?

      the code is already there in the blog for sort by popular.

      • Maha Khalaf

        Can you please post the link to that topic?

        • Manish Prakash

          The sorting function is written in this blog post only:

          public function sortByReview($dir){        $table = $this->getTable(‘review/review’);        $entity_code_id = Mage::getModel(‘review/review’)->getEntityIdByCode(Mage_Rating_Model_Rating::ENTITY_PRODUCT_CODE);        $cond = $this->getConnection()->quoteInto(‘t2.entity_pk_value = e.entity_id and ‘,”).        $this->getConnection()->quoteInto(‘t2.entity_id = ? ‘,$entity_code_id);        $this->getSelect()->joinLeft(array(‘t2’=>$table), $cond,array(‘review’ => new Zend_Db_Expr(‘count(review_id)’)))        ->group(‘e.entity_id’)->order(“review $dir”);    }There should not be any different between sortByReview and sortByPopularity?

          • Maha Khalaf

            to be more precise, I am looking to implement “sort by most viewed”

          • Manish Prakash

            Ok “sort by most viewed” would be more complicated to implement. I will see how to do this and get back to you.

          • Maha Khalaf

            thanks alot

  • Pingback: i like reading()

  • Kieran

    Hi there is a much simpler way to do this now:
    You’ll need to modify the attribute on the backend.

    Go to Catalog >> Attributes >> Manage Attributes. Click on your attribute or create one. Under Frontend Properties (on the first screen, at the very bottom) change Used for sorting in product listing to “Yes.”Go to Catalog >> Manage Categories.Click on the category you want to sort by attribute.Click on the Display Settings tab.Under Default
    Product Listing Sort by, uncheck the “Use Config Settings” and then
    select the attributes from the dropdown. (Obviously if you want this to be the
    default for the entire site, you can change it in System >>
    Configuration >> Sales >> Catalog instead.)

    • NL

      Now for the entire site it’s “System >> Configuration >> Catalog >> Frontend” — Thanks for your help by the way

  • zafer duzen


    I’m getting an error like this “Call to a member function quoteInto() on a non-object in line ..”

    I wrote sortByReview function in overrided Toolbar.php
    is it true or what do you think about my problem,


  • Manish Prakash

    Thanks Mikal 🙂

  • I was able to have it working without overriding and “class”.
    1. Do you edit to setCollection()
    2. Put your public function sortByReview() in “mage/catalog/model/resource/product/Collection.php”
    3. update public function getAttributeUsedForSortByArray() with

    ‘review’ => ‘# of Reviews’ after the ‘position’, line

  • Guest

    Hi Manish,

    I have to sort configurable product by price ?is it possible? if so, pls tel code for tat…

    • shobana

      pls reply me soon

  • kingshuk deb

    How can i add review as a default sorting method in admin panel in “Product Listing Sort by”….suppose i create a custom attribute “popularity”…now i want to add it “Product Listing Sort by” and add it as default method to sort our product…how can i achieve that???….

    below is the question what i ask for

  • Hardik Patel

    I’m trying to Display all products from my store to given position. I did change and set position from category products under manage category section.

    This is my category structure.

    Root category(0)

    Now my problem is given position is not working for root category(Man, Women). Its working ok for all subcategories. Anyone can help me how to solve this problem? My magento ver is 1.6 Thanks,

    Question is also posted here.

  • Brijesh

    Hi manish,
    I am every time use your blog..
    I want bestseller product in sort by option select in category list page..

  • Satyendra

    Hello Manish

    How can i display all the category products in random order on the frontend.

    How can I create a new sort by option for displaying products in random order. Can you please guide how can i do that.

  • Menthos

    Hello! How about if i want to sort the products by a speciffic order (the client has only few products, and wants them to be arranged in a speciffic order different then ASC or DESC.. something like 1,3,5,2,4 or any other random array)? Thank you!

  • flamefox

    Hi Manish! Thanks for the post!
    But how about sorting by presence in wishlists or most viewed? Is it also possible to make with code or should I use an extension for that? I’ve found some good ones, especially improved sorting by amasty. Do you have any suggestions?

    Thanks in advance!


  • Shweta Kaushish

    I have tried it but the custom added options are not displaying in magento admin???Any suggestions??

    • Shweta Kaushish

      Can anybody tell me how to display these custom option in sorting dropdown under display settings tab in magento admin?/

  • Chaitanya Mahamuni

    Hi.. I am facing a issue.. Custom Product sorting **only working on Category Pages**.. I am getting following error on search results page… “Fatal error: Call to undefined method Mage_CatalogSearch_Model_Resource_Fulltext_Collection::sortByReview()”..

    Can anyone give any pointers?