I need to sort my search results by product categories.

If I have Product A and Product B in Category A And I have Product C and Product D in Category B

If all products are available in search result, I want to show them like,

Product A, Product B, Product C, Product D

I want to put same category products with each other on results page.

Currently It is showing as

Product A, Product C, Product B, Product D

I have tried this but its not working.

  • 1
    This would be a phtml edit for the skin/theme of your site. Additionally, I'd recommend some steps to improve performance etc.. Another way to do it would be to create a new attribute for the items in question, and simply populate them manually. However, this would not be easily maintainable or updateable as items are moved around in categories.
    – Kevin Hill
    Commented Apr 6, 2017 at 16:34
  • @KevinHill, Thanks for your reply but I dont want to do it in phtml file because I am using auto complete feature and in that too I want to show products in suggested order.
    – John
    Commented Apr 7, 2017 at 4:45
  • How do you determine the order of the categories? If they are easily sortable, you can just join them to your collection of products in your ajax controller and order by sorting attribute. Commented Apr 12, 2017 at 7:26
  • It doesn't matter if they are in order of the categories, I just want to show same category products beside each other. e.g. First all products from category 1 then all products from category 2, etc.
    – John
    Commented Apr 12, 2017 at 7:35
  • You'll need to extend the search model and apply sorting there. A product can belong to more than one category so you'll need to get only 1 value in category_id and then apply sorting. This is quick idea of implementation, you'll need to get the right file and function to sort the search result collection. Commented Apr 13, 2017 at 13:26

1 Answer 1


The reference you are using is not working is because there is no code to sort the products by category. You need to override Mage_CatalogSearch_Block_Result::_getProductCollection() and then add sorting function in it to show results in order of category id.

Try with below. Use the same reference and add this additional function to the same file.

protected function _getProductCollection()
    if (is_null($this->_productCollection)) {
        $this->_productCollection = $this->getListBlock()->getLoadedProductCollection();
    $listOrder = $this->getRequest()->getParam('dir') ? $this->getRequest()->getParam('dir') : 'desc';
    if(!$this->getRequest()->getParam('order') || $this->getRequest()->getParam('order') == 'category'){
        $this->_productCollection->getSelect()->joinLeft(array('category' => Mage::getSingleton('core/resource')->getTableName('catalog_product_category')),'e.entity_id = category.product_id', array('category_id'))->group('e.entity_id')->order('category.category_id '.$listOrder);
    return $this->_productCollection;
  • Thanks, now I can see same category products beside each other. But changing order is not working for category sort.
    – John
    Commented Apr 14, 2017 at 5:15
  • Please, Check updated answer. Commented Apr 14, 2017 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.