Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Layered Navigation - Sort By Count

I would like to sort each of my layered navigation filters by # of items in each filter.

Here's what shows now-

  • Books - 1
  • CD's - 2
  • DVD's - 20

What I want to show-

  • DVD's - 20
  • CD's - 2
  • Books - 1

I've been looking at catalog/layer/filter.phtml, but I can't figure out how to sort magento collections.

Ideally I want something like this:

$this->getItems()->order('Count Desc')

How can I accomplish this?

like image 420
Brian Avatar asked May 15 '11 14:05

Brian


2 Answers

Found the way to do this-

Modified Mage/Catalog/Model/Layer/Filter/Abstract.php to re-sort using count in the getItems method.

public function getItems()
{
    if (is_null($this->_items)) {
        $this->_initItems();
    }

    //5-16-11 Custom sort
    $items = $this->_items; 
    usort($items, array("Mage_Catalog_Model_Layer_Filter_Abstract", "sortByCount"));  
    return $items;
}

public static function sortByCount($a, $b)
{
    if ($a->getCount() == $b->getCount()) {
        return 0;
    }
    return ($a->getCount() > $b->getCount()) ? -1 : 1;
}
like image 71
Brian Avatar answered Oct 05 '22 15:10

Brian


A good place to start is Mage/Catalog/Model/Layer/Filter/Attribute.php. The list is built there..

like image 26
josephtikva1 Avatar answered Oct 05 '22 15:10

josephtikva1