there is a function

protected function _getProductCollection()
    if (is_null($this->_productCollection)) {
        $layer = $this->getLayer();
        /* @var $layer Mage_Catalog_Model_Layer */
        if ($this->getShowRootCategory()) {

        // if this is a product view page
        if (Mage::registry('product')) {
            // get collection of categories this product is associated with
            $categories = Mage::registry('product')->getCategoryCollection()
                ->setPage(1, 1)
            // if the product is associated with any category
            if ($categories->count()) {
                // show products from this category

        $origCategory = null;
        if ($this->getCategoryId()) {
            $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
            if ($category->getId()) {
                $origCategory = $layer->getCurrentCategory();
        $this->_productCollection = $layer->getProductCollection();


        if ($origCategory) {

    return $this->_productCollection;

It returns the product collection after applying the layered navigation filters.

Anyone could let me know where it is applying the filters.

For example : I have an attribute brand which is shown in layered navigation.

If I select a value for this attribute then Magento refreshes the page and returns the product collection after applying the filter. The above function is returning the product collection but I could not find the query where it is applying this condition.

Anyone could tell me how magento is getting the product collection, where is the query.

So as suggested by @simonthesorcerer

 $this->_productCollection = $layer->getProductCollection();

I looked into the function getProductCollection() and echoed out what was there in the collection. Here is the function

 * Retrieve current layer product collection
 * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
public function getProductCollection()
    if (isset($this->_productCollections[$this->getCurrentCategory()->getId()])) {
        $collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
    } else {
        $collection = $this->getCurrentCategory()->getProductCollection();
        $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
  echo "<pre>";print_r($collection->getData());die();  //Custom to check collection
    return $collection;

I selected a category and got 62 products in collection that was correct and then I selected an attribute filter and again it shows 62 products, means this is not the function which is applying attribute filter to categories. So anyone could tell me where is the code where attribute filter is applied to the collection. I just want to locate that code.

1 Answer 1


As you might have noticed, there is an object of Mage_Catalog_Model_Layer that gets initialized, and later calls getProductCollection(). This is where the layered navigation stuff happens, and you just need to follow that function.

  • according to your answer I have added few details to my question. please have a look Apr 23, 2018 at 9:03

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.