Magento – Filter out attributes used in layered navigation

attributeslayered-navigationmagento-1.9

I have a situation where I have 2 layers of categories, one inside the other

I want to list the same products in the top level category as I use in the 2nd level sub categories. However I only want one set of attributes to show in the top level layered navigation, then another set of attributes in the 2nd level layered navigation. So for example, if I have this structure…

Washing Machines
– Top Loading Washers
– Front Loading Washers
– Washer Dryer Combo

…in the top level 'Washing Machines' layered navigation I want to show the labels for Machine Type as "Top Loading Washers", "Front Loading Washers", "Washer Dryer Combo" then in the second level for each machine type I want to show the layered nav for Machine Capacity as "3kg", "4kg", "6kg".

I don't want to show the Machine Capacity in the top level navigation and I don't want to show the Machine Type in the sub categories. Is it possible to filter out attributes used in layered navigation so only one set is used in the top level and another set is used in the sub category while using the same product in both categories? Thanks

Best Answer

Layered navigation filters are rendered in catalog/layer/view.phtml template:

            <?php foreach ($_filters as $_filter): ?>
            <?php if($_filter->getItemsCount()): ?>
                <dt><?php echo $this->__($_filter->getName()) ?></dt>
                <dd><?php echo $_filter->getHtml() ?></dd>
            <?php endif; ?>

Here you can add checks for category and attribute id (replace the code aabove with the code below):

            <?php 
                $_categoryId = $this->getLayer()->getCurrentCategory()->getId();
                $_categoryFilters = array(
                    // top level categories
                    array(
                        'categories' => array(5,8,10),
                        'attributes' => array('color', 'size'),
                    ),
                    // second level categories 
                    array(
                        'categories' => array(6, 7, 9, 11),
                        'attributes' => array('manufacturer'),
                    ),
                );
            ?>
            <?php foreach ($_filters as $_filter): ?>
            <?php 
                $_found = false;
                foreach ($_categoryFilters as $_categoryFilter) {
                    if (in_array($_categoryId, $_categoryFilter['categories']) &&
                        (!($_attribute = $_filter->getAttributeModel()) ||
                        in_array($_attribute->getAttributeCode(), $_categoryFilter['attributes']))
                    )
                    {
                        $_found = true;
                        break;
                    }
                }
            ?>
            <?php if($_found && $_filter->getItemsCount()): ?>
                <dt><?php echo $this->__($_filter->getName()) ?></dt>
                <dd><?php echo $_filter->getHtml() ?></dd>
            <?php endif; ?>