Magento customized "Shop by" Navigation

http://www.panticz.de/sites/default/files/magento/customized-shop-by/local.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
	/app/design/frontend/default/default/layout/local.xml
-->
<layout>
	<!-- 
		default
	-->
	<default>
		<!--
			head
		-->
		<reference name="head">
			<!-- add custom css -->
			<action method="addCss"><stylesheet>css/local.css</stylesheet></action>
		</reference>
	</default>


	<!-- 
		catalog view
	-->
	<catalog_category_layered>
		<reference name="product_list">
			<action method="setColumnCount"><columns>4</columns></action>
		</reference>

		<reference name="content">
            <!-- override shop by filter -->
			<block type="catalog/layer_view" name="my.catalog.leftnav" before="product_list" template="catalog/layer/mr.view.phtml"/>

            <!-- shop by filter: remove category filter -->
			<reference name="my.catalog.leftnav">
				<action method="unsetChild"><alias>category_filter</alias></action>
			</reference>
		</reference>
	</catalog_category_layered>
</layout>

http://www.panticz.de/sites/default/files/magento/customized-shop-by/layer_view.css

/*
* /skin/frontend/default/default/css/local.css
*/

/* layer_view custom css */
.block-layered-nav-filter {
    clear:both;
}

.block-layered-nav-filter-name,
.block-layered-nav-filter-value {
    float:left;
}

.block-layered-nav-filter-value li {
    float:left;
    padding-top:4px;
    padding-left:14px;
}

http://www.panticz.de/sites/default/files/magento/customized-shop-by/my.view.phtml

<?php
/**
 * Category layered navigation
 *
 * @see Mage_Catalog_Block_Layer_View
 * http://docs.magentocommerce.com/Mage_Catalog/Mage_Catalog_Block_Layer_View.html
 *
 * customized layered navigation
 * ./app/design/frontend/default/default/template/catalog/layer/my.view.phtml
 * based on
 * ./app/design/frontend/base/default/template/catalog/layer/view.phtml
 *
 * v2011-02-08 by panticz
 */
?>

<?php
function is_filter($_filters, $_content = "") {
    if(strlen($_content) > 0) {
       return true;
    } else {
        foreach ($_filters as $_filter) {
            if($_filter->getItemsCount() > 1) {
                return true;    
            }
        }
    }

    return false;
}

/* overide this function to disable filter on some category */
function checkAttribute($_filter) {
    return true;

    /* example: show this filter only in this category
    $category = array(
        "diagonal" => array("TV", "LCD", "Plasma"),
        "resolution" => array("Beamer")
    );

    $currentCategory = Mage::registry('current_category');

    if($_filter->getAttributeModel() != null) {
        $attributeCode = $_filter->getAttributeModel()->getAttributeCode();

        // check if filter for attribute exists
        if (array_key_exists($attributeCode, $filter)) {
            // find top category name
            while($currentCategory->getLevel() > 3) {
                $currentCategory = $currentCategory->getParentCategory();
            }

            // check if top category in filter
            if(in_array($currentCategory->getName(), $filter[$attributeCode])) {
                return true;
            } else {
                return false;
            }
        }
    }
        
    return true;
    */
}

/* overide this function to disable filter on some pages */
function showFilter() {
    return true;

    /* example: dont show filter in root category
    if(Mage::registry('current_category')->getUrlPath() == "shop.html") {
        return false;
    } else {
        return true;
    }
    */
}
?>

<?php if($this->canShowBlock() && showFilter()): ?>
    <?php $_filters = $this->getFilters() ?>
    <?php if(is_filter($_filters, $this->getStateHtml())): ?>
        <div class="block block-layered-nav">
            <div class="block-content">
            <?php echo $this->getStateHtml() ?>
            <?php if($this->canShowOptions()): ?>
                <?php if(is_filter($_filters)): ?>
                    <p class="block-subtitle"><?php echo $this->__('Shopping Options') ?></p>
                    <?php foreach ($_filters as $_filter): ?>
                        <?php if($_filter->getItemsCount() > 1 && checkAttribute($_filter)): ?>
                            <div class="block-layered-nav-filter">
                                <div class="block-layered-nav-filter-name">
                                    <ol class="currently">
                                        <li><span class="label"><?php echo $this->__($_filter->getName()) ?>:</span></li>
                                    </ol>
                                </div>
                                <div class="block-layered-nav-filter-value"><?php echo $_filter->getHtml() ?></div>
                            </div>
                        <?php endif; ?>
                    <?php endforeach; ?>
                <?php endif; ?>
            <?php endif; ?>
            </div>
        </div>
    <?php endif; ?>
<?php endif; ?>

http://www.panticz.de/sites/default/files/magento/customized-shop-by/filter.phtml

<?php
    if (strrpos($this->helper('core/url')->getCurrentUrl(), "catalogsearch")) {
        include($_SERVER["DOCUMENT_ROOT"] . "/app/design/frontend/base/default/template/catalog/layer/filter.phtml");
    } else {
        include("filter.select.phtml");
    }
?>

http://www.panticz.de/sites/default/files/magento/customized-shop-by/filter.select.phtml

<?php
/**
 * Template for filter items block
 *
 * @see Mage_Catalog_Block_Layer_Filter
 * http://docs.magentocommerce.com/Mage_Catalog/Mage_Catalog_Block_Layer_Filter_Attribute.html
 *
 * ./app/design/frontend/default/default/template/catalog/layer/filter.phtml
 * based on
 * ./app/design/frontend/base/default/template/catalog/layer/filter.phtml
 */
?>

<script type="text/javascript">
<!--
function magejump(targ, selObj, restore) {
    eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
    if (restore) {
        selObj.selectedIndex = 0;
    }
}
//-->
</script>

<select name="magestp" id="magestp" onChange="magejump('parent', this, 0)">
    <option value=""><?php echo $this->__('All') ?></a></option>
    <?php foreach ($this->getItems() as $_item): ?>
    <option value="<?php echo $this->urlEscape($_item->getUrl()) ?>"><?php echo $_item->getLabel() ?> (<?php echo $_item->getCount() ?>)</option>
    <?php endforeach ?>
</select>

http://www.panticz.de/sites/default/files/magento/customized-shop-by/state.phtml

<?php
    if (strrpos($this->helper('core/url')->getCurrentUrl(), "catalogsearch")) {
        echo "";
        //include($_SERVER["DOCUMENT_ROOT"] . "/app/design/frontend/base/default/template/catalog/layer/state.phtml");
    } else {
        include("state.select.phtml");
    }
?>

http://www.panticz.de/sites/default/files/magento/customized-shop-by/state.select.phtml

<?php 
/**
 * Category layered navigation state
 *
 * @see Mage_Catalog_Block_Layer_State
 * http://docs.magentocommerce.com/Mage_Catalog/Mage_Catalog_Block_Layer_State.html
 *
 * ./app/design/frontend/default/default/template/catalog/layer/state.phtml
 * based on
 * ./app/design/frontend/base/default/template/catalog/layer/state.phtml
 *
 * v2011-02-08 by panticz
 *
 */
?>

<?php $_filters = $this->getActiveFilters() ?>
<?php if(!empty($_filters)): ?>
    <script type="text/javascript">
    <!--
    function magejump(targ, selObj, restore) {
        eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
        if (restore) {
            selObj.selectedIndex = 0;
        }
    }
    //-->
    </script>

    <p class="block-subtitle active"><?php echo $this->__('Currently Shopping by') ?>
        <span><a href="<?php echo $this->getClearUrl() ?>" class="btn-remove" title="<?php echo $this->__('Clear All') ?>"><?php echo $this->__('Clear All') ?></a></span>
    </p>
    <?php foreach ($_filters as $_filter): ?>
        <ol class="currently">
            <li>
                <span class="label"><?php echo $this->__($_filter->getName()) ?>:</span>
            </li>
        </ol>
        <select name="magestp" id="magestp" onChange="magejump('parent', this, 0)">
            <option><?php echo $_filter->getLabel() ?></option>
            <option value="<?php echo $_filter->getRemoveUrl() ?>"><?php echo $this->__('All') ?></a></option>
        </select>
    <?php endforeach; ?>
<?php endif; ?>

OPTIONAL: state.phtml with filter
ToDo: HowTo get avaiable options in a category for a filter?
http://www.panticz.de/sites/default/files/magento/customized-shop-by/state.phtml.with.filter.dev

<?php 
/**
 * Category layered navigation state
 *
 * @see Mage_Catalog_Block_Layer_State
 * http://docs.magentocommerce.com/Mage_Catalog/Mage_Catalog_Block_Layer_State.html
 *
 * ./app/design/frontend/default/default/template/catalog/layer/state.phtml
 * based on
 * ./app/design/frontend/base/default/template/catalog/layer/state.phtml
 */
?>

<?php $_filters = $this->getActiveFilters() ?>
<?php if(!empty($_filters)): ?>
    <script type="text/javascript">
    <!--
    function magejump(targ, selObj, restore) {
        eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
        if (restore) {
            selObj.selectedIndex = 0;
        }
    }
    //-->
    </script>

    <p class="block-subtitle active"><?php echo $this->__('Currently Shopping by') ?><span><a href="<?php echo $this->getClearUrl() ?>" class="btn-remove" title="<?php echo $this->__('Clear All') ?>"><?php echo $this->__('Clear All') ?></a></span></p>
    <?php foreach ($_filters as $_filter): ?>
        <ol class="currently">
            <li>
                <span class="label"><?php echo $this->__($_filter->getName()) ?>:</span>
            </li>
        </ol>
        <select name="magestp" id="magestp" onChange="magejump('parent', this, 0)">
            <option value="<?php echo $_filter->getRemoveUrl() ?>"><?php echo $this->__('All') ?></a></option>

            <?php
                $attributeCode = $_filter->getFilter()->getRequestVar();
                $product = Mage::getModel('catalog/product');



                $collection = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($attributeCode);
                // ->setEntityTypeFilter($product->getResource()->getTypeId())
                $_attribute = $collection->getFirstItem()->setEntity($product->getResource());
                $attribute_options = $_attribute->getSource()->getAllOptions(false);



/*
$layer = Mage::getModel("catalog/layer");
//foreach($categories as $categoryid) {
//    $category = Mage::getModel("catalog/category")->load($categoryid);
$category = Mage::registry('current_category');
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();
    echo "xxxxxxxxxxxx" . print_r($attributes);
//}
//$_activefilters = Mage::getSingleton(’Mage_Catalog_Block_Layer_State’)->getActiveFilters();
*/
            ?>

            <?php foreach($attribute_options as $option): ?>
                <?php $optionLabel = $option['label']; ?>
                <?php $optionValue = $option['value']; ?>
                <?php if(strrpos($_filter->getRemoveUrl(), "?")) $token = "&"; else $token = "?"; ?>
                <?php $link = $token . $attributeCode . "=" . $optionValue; echo $link; ?>
                <option value="<?php echo ($_filter->getRemoveUrl()) . $link; ?>"
                <?php if($optionLabel == $_filter->getLabel()) echo " selected"; ?>><?php echo $optionLabel // $_filter->getLabel() ?></option>
            <?php endforeach; ?>
        </select>
    <?php endforeach; ?>
<?php endif; ?>

other files
./app/code/core/Mage/Catalog/Block/Layer/Filter/Abstract.php
./app/design/frontend/default/default/layout/catalogsearch.xml

Links
http://classyllama.com/development/magento-development/the-better-way-to-modify-magento-layout/
http://docs.magentocommerce.com/Mage_Catalog/Mage_Catalog_Block_Layer_Filter_Attribute.html
http://www.mageintern.de/magento-shop-design/magento-navigation/filternavigation-layered-nav-mit-drop-down-auswahl.html
http://www.magentocommerce.com/boards/viewthread/28220/

Do you like this page? Then support it. Please click the AD below and visit the sponsor. Thank you!