Layered Navigation Filter – How to Show All the Time

attributesfilterlayered-navigationmagento-1.9

I have seen lots of people who discussed about this issue, also given plenty of ideas, but i am still stuck at the issue. As per the answer at this link, i have written the new function to get the active filters value at view.php

app/code/local/Mage/Catalog/Block/Layer/View.php,

public function getActiveFilters()
    {
        $filters = $this->getLayer()->getState()->getFilters();
        foreach($filters as $filter){
            $active[] = $this->getChild($filter->getFilter()->getRequestVar().'_filter');
        }
        return $active;
    }

this function gives the value of the current filter name,label.

at view.phtml,

$_active = $this->getActiveFilters();

foreach ($_active as $active) {?>

   <select class="filter_box col-xs-12" onchange="filter(this.value)">
    <option value="#"><?php echo $this->__($active->getName()) ?></option>
    <?php echo $active->getHtml() ?>
    </select>
<?php }?>

In this link, @B00MER mentioned extending the filter class will solve the issue also provided the code for the Category Filter. I have checked it at my magento site, but there is no changes. It is just displaying the filter without any option.

I don't know how to achieve it, also don't know the method which i am using ,is correct or not.

Can someone point me the right solution for this issue.

Thanks.

Best Answer

To show filters all time in layered navigation, I have rewritten the magento catalog core classes, For Attribute Filters, rewrite class Mage_Catalog_Model_Resource_Layer_Filter_Attribute,

replace the following line at applyFilterToCollection function

$tableAlias = $attribute->getAttributeCode() . '_idx';

by,

$tableAlias = $attribute->getAttributeCode() . '_idx_'.$value;

rewrite the Mage_Catalog_Model_Layer_Filter_Attribute,

Comment Out the $this->_items = array(); at the function apply,

public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
    {
        $filter = $request->getParam($this->_requestVar);
        if (is_array($filter)) {
            return $this;
        }
        $text = $this->_getOptionText($filter);
        if ($filter && strlen($text)) {
            $this->_getResource()->applyFilterToCollection($this, $filter);
            $this->getLayer()->getState()->addFilter($this->_createItem($text, $filter));
            //$this->_items = array();
        }
        return $this;
    }

rewrite the Mage_Catalog_Model_Layer_Filter_Item to add the following method,

public function isSelected(){
        $selected = Mage::getSingleton('core/app')->getRequest()->getParam($this->getFilter()->getRequestVar());

        if($selected == $this->getValue()){
            return true;
        }else{
            return false;
        }
    }

isSelected() used for getting attribute clear url at the filter.phtml

The above steps will help to show all attributes at all time.

Ref:https://dhimandey.wordpress.com/2012/11/27/show-all-attributes-after-filter-in-layered-navigation/

For Price Filter,

rewrite the Mage_Catalog_Model_Layer_Filter_Price, comment out the else condition at the _getItemsData

protected function _getItemsData()
    {
        if (Mage::app()->getStore()->getConfig(self::XML_PATH_RANGE_CALCULATION) == self::RANGE_CALCULATION_IMPROVED) {
            return $this->_getCalculatedItemsData();

        } /*elseif ($this->getInterval()) {
            //return array();
        }*/

        $range      = $this->getPriceRange();
        $dbRanges   = $this->getRangeItemCounts($range);
        $data       = array();

        if (!empty($dbRanges)) {
            $lastIndex = array_keys($dbRanges);
            $lastIndex = $lastIndex[count($lastIndex) - 1];

            foreach ($dbRanges as $index => $count) {
                $fromPrice = ($index == 1) ? '' : (($index - 1) * $range);
                $toPrice = ($index == $lastIndex) ? '' : ($index * $range);

                $data[] = array(
                    'label' => $this->_renderRangeLabel($fromPrice, $toPrice),
                    'value' => $fromPrice . '-' . $toPrice,
                    'count' => $count,
                );
            }
        }

        return $data;
    }

This will helps to retain the price filter at all time. I hope this answer will help someone.

Related Topic