Tweakmag - 03 April 2008 04:47 PM
Hi there,
it seems a few people want to control the way layered navigation works, here is some code that will let you:
1. Define any attributes that you want to be displayed as a dropdown list
2. Set a threshold limit for the attributes, so that if this threshold is met, then automatically display as a dropdown list
This code needs to go into “app/design/frontend/default/[YOURTHEME]/template/catalog/layer/filter.phtml
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* @category design_default
* @package Mage
* @copyright Copyright (c) 2004-2007 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
*/
/**
* Template for filter items block
* Coded by Adam Martin (www.tweakmag.com)
*
*
* @see Mage_Catalog_Block_Layer_Filter
*/
?>
<?php
//control the way that the layered navigation attributes present themselves
//either dropdown list or default methd (ordered list)
$attributeName = $this->getName();
$itemcountthreshold = 2; // you can change this
$itemcount = $this->getItemsCount();
$displayitemcount = false; //set to true/false to display item count in brackets
if($itemcount > $itemcountthreshold){
$attributeName = "Overthreshold";
}
if(!function_exists("_displayOrderedlist")){
function _displayOrderedlist($atts,$displayitemcount){
echo '<ol>';
foreach($atts->getItems() as $_item){
echo '<li><a href="'.$_item->getUrl().'">'.$_item->getLabel().'</a>';
if($displayitemcount){
echo ' ('.$_item->getCount().')';
}
echo '</li>';
}
echo '</ol>';
}
}
if(!function_exists("_displayDropdown")){
function _displayDropdown($atts,$displayitemcount){
echo '<select id="layered-select" class="select" name="layered-select" onchange="if (this.selectedIndex > 0) location.href=this[this.selectedIndex].value;">';
echo '<option selected="selected">Please select</option>';
foreach ($atts->getItems() as $_item){
echo '<option value="'.$_item->getUrl().'">';
echo $_item->getLabel();
if($displayitemcount){
echo ' ('.$_item->getCount().')';
}
echo '</option>';
}
echo '</select>';
}
}
switch ($attributeName) {
case 'Shoe Size':
case 'Overthreshold':
_displayDropdown($this,$displayitemcount);
break;
default:
_displayOrderedlist($this,$displayitemcount);
break;
}
?>
You will see that I have added case ‘Shoe Size’ - by doing this anytime the Shoe Size attribute is displayed it will be displayed using a dropdown. For example you could add:
case 'Price':
to make price always display as a dropdown.
Cheers
Adam