Magento – Bespoke attribute filters for each category pages

filterlayered-navigationmagento-1.9

By default Magento will display every attribute with a status of 'Filterable' on layered navigation. I want to be able to decide which filterable attributes appear on each category page.

It's important to note that I need the same product to display different filterable attributes on different category pages. It's also important to note that a category may contain products from several different attribute sets, so I cannot do this via Attribute sets.

For example, let's say I have attributes A, B and C and I have two category pages, CAT1 and CAT2. By default the filters would appear:

CAT1

  • A (option 1, 2 etc)
  • B (option 1, 2 etc)
  • C (option 1, 2 etc)

CAT2

  • A (option 1, 2 etc)
  • B (option 1, 2 etc)
  • C (option 1, 2 etc)

I want the option to be able to say CAT1 should only display attribute B and C whilst CAT2 should display A and C; thus it would appear:

CAT1

  • B (option 1, 2 etc)
  • C (option 1, 2 etc)

CAT2

  • A (option 1, 2 etc)
  • C (option 1, 2 etc)

Again, it's important to note that the two categories could contain exactly the same products…it's just that I want to be able to decide which attributes can be filtered depending on the category.

In the actual site I have > 100 categories and > 3000 products. There's also an admin team so whatever solution I implement can't be hard coded – the users have to be able to amend for each category page.

Anyone heard of any solution like this? Or is this a bespoke bit of development work I'm looking at here?

Best Answer

This is a broad subject. Most probably you won't get a fully working answer, but I can give you some tips on where to start.

I would add 2 more category attributes:

One being a yes/no attribute called Use custom filter attributes with the code use_custom_filters. If this is set to no then all the available attributes will be used in the filters.

The second attribute should be a multiselect with all the attributes available for filtering. let's call it Attributes for filter with code filter_attributes.

You can get the product attributes available for filtering like this:

$collection = Mage::getResourceModel('catalog/product_attribute_collection')->addFieldToFilter('is_fiterable', 1);  

Here is a tutorial on how to add a category attribute.
Also here is a tutorial on how to add an attribute with custom options. The example is for products, but it works the same for categories. Just replace catalog_product with catalog_category.

Now you need to change the functionality that determines the filterable attributes.
This happens in the layer model: https://github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/core/Mage/Catalog/Model/Layer.php#L215.

You just need to rewrite that method and check if you are in a category context (if Mage::registry('current_category') returns something other than null).
then check this ($collection is the variable in the method mentioned):

if (Mage::registry('current_category')->getUseCustomFilters()) {
    $allowedAttributeIds = Mage::registry('current_category')->getFilterAttributes();
    $collection->addFieldToFilter('attribute_id', array('in' => $allowedAttributeIds))
} 
//else, do nothing, let the script behave as it does.

Not sure if getFilterAttributes will return an array or a string. If it returns a string you just have to explode it by comma before using it.