Magento Layered Navigation - Sort By Count
Asked Answered
C

2

5

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?

Canaanite answered 15/5, 2011 at 14:39 Comment(0)
C
5

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;
}
Canaanite answered 16/5, 2011 at 16:41 Comment(1)
It's Mage/Catalog/Model/Layer/Filter/Abstract.php. Not Mage/Catalog/Layer/Filter/Abstract.phpBlase
L
3

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

Longo answered 15/5, 2011 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.