Magento – Hide certain filter from layered navigation top category

layered-navigation

I have the following question:

Add the layerend navigation of a top category, all the filters of all underlying subcategories are shown. In top categories, we only want to show two filters: product type and brand.

We experimented in view.phtml based on other topics, but haven't found the exact result.

It would be great if you could point us towards the solution!

Best Answer

This is what I understood from your question. You want to remove category filter for your two top categories (for them, isAnchor is set to Yes) and keep all other filters as it is.

To achieve this, most easiest solution is a category specific layout update via any of your layout update xml file. For example, your category id be 12. Then add below code in your local.xml file.

File : app\design\frontend\[package]\[theme]\layout\local.xml

<layout>
    <CATEGORY_12> 
        <reference name="catalog.leftnav">  
        <action method="unsetChild"><alias>category_filter</alias></action>
        </reference>
    </CATEGORY_12>
</layout>

Here CATEGORY_12 is a layout handle update which is specifically created for a category with an id 12. Inside that, we are basically removing category filter from layered navigation.

Hope you get the idea.

EDIT

So you want to remove category filter and keep all other filters intact. To do that, I believe you need to rewrite the class Mage_Catalog_Block_Layer_View which you can find at app/code/core/Mage/Catalog/Block/Layer/View.php

protected function _prepareLayout()
{
    $stateBlock = $this->getLayout()->createBlock($this->_stateBlockName)
        ->setLayer($this->getLayer());

    $categoryBlock = $this->getLayout()->createBlock($this->_categoryBlockName)
        ->setLayer($this->getLayer())
        ->init();

    $this->setChild('layer_state', $stateBlock);
    $this->setChild('category_filter', $categoryBlock);

    //CATEGORY IDS IN WHICH YOU WANT TO HIDE OTHER FILTERS
    $excludeCategories = array(12, 13); //example
    $currentCategory   = Mage::registry('current_category');

    //avoid rendering attribute filters for exclude categories
    if (!in_array($currentCategory->getId(), $excludeCategories)) {
        $filterableAttributes = $this->_getFilterableAttributes();
        foreach ($filterableAttributes as $attribute) {
            if ($attribute->getAttributeCode() == 'price') {
                $filterBlockName = $this->_priceFilterBlockName;
            } elseif ($attribute->getBackendType() == 'decimal') {
                $filterBlockName = $this->_decimalFilterBlockName;
            } else {
                $filterBlockName = $this->_attributeFilterBlockName;
            }

            $this->setChild($attribute->getAttributeCode() . '_filter',
                $this->getLayout()->createBlock($filterBlockName)
                    ->setLayer($this->getLayer())
                    ->setAttributeModel($attribute)
                    ->init());
        }
    }

    $this->getLayer()->apply();

    return parent::_prepareLayout();
}

Use your category ids in $excludeCategories and you are good to go.

Related Topic