Search code examples
magentofiltersubmenu

How to bypass or reset a filter manually?


I want to show filter of category on submenu, my code works!!

My problem is that if page are already filtered, my code does not return the options

I believe it has to do something in the code that bypasses the filter page and again bring the options in the submenu even if already have the filter on page

HTML of submenu:

  {{block type="core/template" category="3" template="page/html/icons_submenu.phtml"}}

Content of page icons_submenu.phtml:

<?php
    $layer = Mage::getModel("catalog/layer");
    $category = Mage::getModel('catalog/category')->load($this->getCategory());
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();

    foreach ($attributes as $attribute) {
        if ($attribute->getAttributeCode() == 'color') {
            $filterBlockName = 'catalog/layer_filter_attribute';
            $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
            echo '<strong>Color:</strong><br />';

            foreach($result->getItems() as $option) {
               echo '&nbsp; &nbsp; &nbsp; <a href="' . $category->getUrl() . '/?color=' . $option->getValue() . '">' . $option->getValue() . ' - ' . $option->getLabel() . '</a><br />';
            }
        }
    }
?>

Example:enter image description here

enter image description here


Solution

  • I would really suggest you to actually move all that logic into a proper module, a proper block and a proper model and not in a template like you are doing right now.

    If you actually want further help for that, feel free to ask, making something according to the coding guide lines of Magento would make you even happier of your job, I can assure you.

    This being said, what you actually want is a current filter model based on the current category and a specify attribute.

    You don't need to go by the block catalog/layer_filter_attribute in a way to do this, you can directly go by the model based on the layer you already load.

    So, this way of doing it should work, although it should not be in a template or view, once again :

    <?php
        $category = Mage::getModel('catalog/category')
                         ->load($this->getCategory());
        $layer = Mage::getModel('catalog/layer')
                     ->setCurrentCategory($category);
        $attributes = $layer->getFilterableAttributes();
    
        foreach ($attributes as $attribute) {
            if ($attribute->getAttributeCode() == 'color') {
                // $filterBlockName = 'catalog/layer_filter_attribute';
                /** This is actually your only problem in your code **/
                // $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
                /** But would work with this line **/
                $result  = Mage::getModel('catalog/layer_filter_attribute')
                               ->setLayer($layer)
                               ->setAttributeModel($attribute);
    
                echo '<strong>Color:</strong><br />';
    
                foreach($result->getItems() as $option) {
                   echo '&nbsp; &nbsp; &nbsp; <a href="' . $category->getUrl() . '/?color=' . $option->getValue() . '">' . $option->getValue() . ' - ' . $option->getLabel() . '</a><br />';
                }
            }
        }
    ?>
    

    Then you can see it still works based on only the colours I have in the current category

    Working without the filtering

    But also when the category is already filtered on a specific colour

    Working with the filtering