How to show all filter values ​​after filter one of them is magenta?

I have a problem. I want to show all the parameters of an attribute after the filtering process is complete.

Let's assume there are 3 filtered colors.

1 Red(5)
2 Blue(6)
3 Green(10)

      

Now suppose I click on the Green (10) link from the store by block, it will display all products containing green. It's corrent, but after getting the filtering results, the other color options weren't showing again, but when I clear the filters it displays all the attribute options, but that's not how I want it to work.

Instead, all color options should be shown even if the filter is active.

like one time when I click the Green (10) link after the filtering process is complete, I want to show all the color options again!

similar to the filtration process

like this

1 Red(5)
2 Blue(6)
3 Green(10)

      

How should I approach this problem?

+3


source to share


1 answer


There are two things to do here:

  • Show selected filter in layered navigation
  • Show all options for this filter

In order to display the selected filter in the layered navigation, we will need to rewrite Mage_Catalog_Model_Layer_Filter_Attribute

.

To show each option of the selected filter, we will have to rewrite Mage_Catalog_Model_Resource_Layer_Filter_Attribute

which handles the filters.

To do this, create a module and in etc / config.xml add the rewrite directive for these models (in the global section):

<models>
    <catalog>
        <rewrite>
            <layer_filter_attribute>Mycompany_Mymodule_Model_Layer_Filter_Attribute</layer_filter_attribute>
        </rewrite>
    </catalog>
    <catalog_resource>
        <rewrite>
            <layer_filter_attribute>Mycompany_Mymodule_Model_Resource_Layer_Filter_Attribute</layer_filter_attribute>
        </rewrite>
    </catalog_resource>
</models>

      

So we create a model file Attribute.php

in Model/Layer/Filter

We just rewrite the method apply

:

class Mycompany_Mymodule_Model_Layer_Filter_Attribute extends Mage_Catalog_Model_Layer_Filter_Attribute
{
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));
            // COMMENT OUT THIS LINE TO AVOID EMPTYING CURRENT ATTRIBUTE
            // $this->_items = array();
        }
        return $this;
    }
}

      



Now create a model file Attribute.php

atModel/Resource/Layer/Filter

Here we will rewrite the method getCount

to show each filter option.

Magento hides other parameters because there is no result for them. The shirt cannot be red and white at the same time for the dropdown attribute. Thus, the red product count is always 0 if white is selected.

So what we're doing is a Magento trick to make it believe there are results for these options.

Here's a rewrite for the method getCount

:

class Mycompany_Mymodule_Model_Resource_Layer_Filter_Attribute extends Mage_Catalog_Model_Resource_Layer_Filter_Attribute
{
    public function getCount($filter)
        {
            // clone select from collection with filters
            // COMMENT OUT ORIGINAL LINE
            // $select = clone $filter->getLayer()->getProductCollection()->getSelect();
            // AND REWRITE USING SELECT FROM ORIGINAL CATEGORY COLLECTION INSTEAD OF FILTERED ONE
            $select = clone $filter->getLayer()->getCurrentCategory()->getProductCollection()->getSelect();
            // BELOW IS THE SAME AS ORIGINAL METHOD
            // reset columns, order and limitation conditions
            $select->reset(Zend_Db_Select::COLUMNS);
            $select->reset(Zend_Db_Select::ORDER);
            $select->reset(Zend_Db_Select::LIMIT_COUNT);
            $select->reset(Zend_Db_Select::LIMIT_OFFSET);

            $connection = $this->_getReadAdapter();
            $attribute  = $filter->getAttributeModel();
            $tableAlias = sprintf('%s_idx', $attribute->getAttributeCode());
            $conditions = array(
                "{$tableAlias}.entity_id = e.entity_id",
                $connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()),
                $connection->quoteInto("{$tableAlias}.store_id = ?", $filter->getStoreId()),
            );

            $select
            ->join(
                array($tableAlias => $this->getMainTable()),
                join(' AND ', $conditions),
                array('value', 'count' => new Zend_Db_Expr("COUNT({$tableAlias}.entity_id)")))
                ->group("{$tableAlias}.value");

            return $connection->fetchPairs($select);
        }
    }
}

      

Basically what we've been doing here is telling Magento to count from the original collection of category products, not layered. Thus, the count is always the same as the original category. (Of course, now they don't make much sense if you mix them with other attributes.)

Et voilà! It should work as intended.

+2


source







All Articles