Reorganize your Pseudo-boolean Filters With Demac / ReorganizeFilters in Magento Community

Sometimes when we’re developing a site for Magento we’ll have a client ask us for specific “one off” filters to use in the layered navigation. These filters include things like “on sale”, “new arrivals”, or “in stock”.

We’ve created these filters as attributes with type “dropdown” in the backend, added one option on each with the text of the filter (example: “In Stock”) and made them filterable in the catalog and in search. You’d think to make these attributes “yes/no” as they’re boolean (something’s either in stock or it’s not) however that attribute type is not filterable in Magento. The problem with these filters by default is they have their own heading, and we wanted them to be grouped together and at the bottom of our list of filters.

DOWNLOAD THE MODULE HERE

Instead of hacking up templates, we found a way of overriding part of the filter blocks to reorganize things they way we wanted them. Introducing Demac / ReorganizeFilters! The main logic is in the Demac_ReorganizeFilters_Block_Layer_View class, where we override the getFilters() function. Essentially the flow of this rewrite works like this:

1. Obtain all of the active “state” filters in the layered navigation

2. Run through all filters available in the layered navigation and see if any “qualify” for being reorganized with the function _getRfFilterCode(). You’ll see in this function that it’s checking if there’s only one option available, and this will qualify it for being a filter to reorganize. (This is our pseudo-boolean logic.) There’s also a commented out section below that if you’d like to hardcode a number of attribute codes to use / reorganize.

3. We take the first reorganized filter we find and change its name and gather its HTML output – this becomes our “availability” filter which is the one that groups the other reorganized filters. All subsequent filters that pass through here will just add their HTML / dropdown options to this filter.

4. We will then progress through the active filters to make sure the current reorganized filter is not an active state filter. If it is, we need to set the items count to zero – which is the logic behind if it’s displayed or not. (If it has more items than zero, it will appear.) Obviously if a filter’s already been applied we don’t want it to still be available in the layered navigation.

5. We want to then unset each “reorganized filter” so it doesn’t appear like in the image above.

6. Finally, we add the “availability” filter to the end of the filters array.

The Demac_ReorganizeFilters_Block_Layer_Filter_Attribute class is important as well as it contains functions to provide a filter’s data if it has any – as we’re setting a filter’s name, HTML, and item’s count. If that data doesn’t exist, you can call Mage_Catalog_Block_Layer_Filter_Attribute’s functions to calculate that data.

This module is for Magento Community.

Enjoy! You can download the module here.