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

     

    Open ./app/design/frontend/default/default/template/catalogue/layer/view.phtml

  • 2

     

    Add

     <?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 before getStateHtml() ?>

  • 3

     

    Replace the contents of

    <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

     

    Change $_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.

This entry was posted in Magento and tagged , , , . Bookmark the permalink.

5 Responses to Dependant layered navigation filters in Magento

  1. this works great to show filter B after filter A has been selected

    but….

    is there a way to only show filters B and C after A has been selected?

    Reply


  2. If you’re using solr with Enterprise you want to do:

    Mage::getSingleton(‘enterprise_search/catalog_layer’)->getState()->getFilters()

    In order to get the active filters.

    Reply



  3. Paul Young

    Hi,
    Is it possible to adapt this solution to only show certain filters if a user in in a particular category?

    Reply


    • Of course it is – anything with Magento is possible, its just a case of time and money. But this is more of a ‘working concept’ than production solution.

      In practice, we don’t perform any logic in the template files, but in the relevant blocks/models that precede those.

      Reply


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">