Dependant layered navigation filters in Magento
Although not the best method, we wrote a quick and easy means to remove layered navigation filters that are dependant on other filters being enabled. This code should really be in the block and popped into an extension - with the attributes administrable via the backend, but for the purposes of this tutorial - we are just going to hardcode it into the template and let you decide how you want to improve it.
-
1
./app/design/frontend/default/default/template/catalogue/layer/view.phtml
-
2
<?php /*Create filter dependencies*/ $_activeFilters[] = array(); $_filters = Mage::getSingleton('Mage_Catalog_Block_Layer_State')->getActiveFilters(); $_depends = array( "fits product" => array("product manufacturer") ); foreach ($_filters as $_filter): $_activeFilters[] = strtolower($_filter->getName()); endforeach; ?>
just beforegetStateHtml() ?>
-
3
<dl id="narrow-by-list"> ... </dl>
with<?php $_filters = $this->getFilters() ?> <?php foreach ($_filters as $_filter): ?> <?php if($_filter->getItemsCount()): ?> <?php /*Hide filters whose dependencies are not enabled*/ $_showFilter = true; if(isset($_depends[strtolower($_filter->getName())])): $_filterDepends = &$_depends[strtolower($_filter->getName())]; foreach ($_filterDepends as $_filterDepend): if (!in_array($_filterDepend,$_activeFilters) && $_showFilter) { $_showFilter = false; } endforeach; endif; ?> <?php if($_showFilter): ?> <dt><?php echo $this->__($_filter->getName()) ?></dt> <dd> <?php echo $_filter->getHtml() ?> </dd> <?php endif; ?> <?php endif; ?> <?php endforeach; ?>
-
4
$_depends
in step 1 to reflect the names of the attributes that have dependencies on each other.$_depends = array( "fits product" => array("product manufacturer") );
Ie. In our example, we do not want to display fits product until product manufacturer has been filtered.