(734)545-8017

The Human Element Blog

Magento Attributes: Using Different Filterable Attributes Per Category

Posted Oct 31, 2011 by

magento-cat
Product attributes within Magento are one of the most powerful tools in the arsenal that both developers and store owners alike can employ to create specific, guided, and relevant user experiences.

With that said, the out-of-the-box functionality of the Magento layered navigation is not always going to do the trick. We recently had a client come to us with a special request to use different product attribute groups dependent upon the category.

Normally this would be possible by specifying a different Attribute Set for products contained within a specific category. However, what if the products all use the same Attribute Set and span a variety of different categories? The answer becomes a little trickier, but we can solve it with some simple template changes.

First, you will need to login to your Magento store admin and navigate to:

Catalog → Attributes → Manage Attributes:
admin-manage-attributes

You will need to create two attributes that you will use as filterable options, one for the category that you want to have different filterable options, and one for all the other categories. Make sure that you have set options for the attribute to be filterable and products have some of the attributes applied to them. If you have any doubts about setting up your attributes, refer to the next two screenshots as examples.

attribute-screen1
attribute-screen2

Once I create my two attribute groups I can see that in all categories both refinement options are displayed:
both-refinements

Before I begin to dig into the template code, I need to find the Category ID’s of the categories that I want to modify. If you navigate to:

Catalog → Manage Categories

Click on the categories to find the Category ID in parenthesis next to the category name. Also check to make sure that the category is set to “Is Anchor: YES” in the display settings.

Now that I have both of my attribute groups created and I know the Category IDs in question I can start to dig into the templates and work some magic on the display.

In your filesystem, navigate to: app/design/frontend/YourPackage/YourPackage/template/catalog/layer/view.phtml

You will see the default code for the layered navigation:
catalog / layer / view.phtml

First add in a variable to store the current category ID and a check for a parent category ID at the top of the page, like this:
Store Variables

Now take a look at the following code:
This is the new code to allow filtering attributes by category ID

Where you see:
These are the pieces of the code you should change to match your store setup

Change the ID number to the ID of the category that you want to display the different filterable attribute group. So when the page loads php will check the category ID of the current category or the parent category. If it is a match it will then check and display the attributes in the appropriate group.

In the next else statement, change “Gift Options” to the Admin name of the filterable attributes that you want to display on all the other category layered navigation pages.

Put it all together

Voila! That’s pretty much it. Now, when you navigate to the front end you will see the Attributes with filtered by name according to how you set it up.

A note of warning! If you delete categories and recreate them you will have to come back to this template and change the ID’s appropriately. It is not always a good idea to modify templates to check for specific IDs if you can avoid it, but this is a handy solution in a pinch.

If you have any questions or run into any issues just post a comment and I’ll try to respond and help out.

Happy Coding!

Danny2
-The Office Minstrel

Tags: Magento

Posted By

Director of Development

Comments (3)

Navdeep posted

Hello, Many thanks for writing this tutorial.I am new to magneto and facing a similar issue but my subcategory level is quite deep.Suppose we have a main category Electronics ,then a subcategory Mobiles and further we have a subcategories like motorola,samsung,sony inside Mobiles. So I need different filterable attributes for electronics ,then different for mobiles and different filters for further subcategories. So should I opt for similar approach like yours? I understand you guy might be very busy and I would really appreciate it if you can guide me into right direction. Thanks.

    Paul Briscoe posted

    @Navdeep - I think you could probably use a similar approach if you're not opposed to hard coding the category ids into the template. If this is a one time thing and will never change you might consider it, otherwise you might set some kind of category attribute to check on to see which attribute sets you should use for filtering. So for example, if you use the proposed solution in this post for the top level category, you could likely store the sub category id in a similar way and modify the template to check the category id and filter again to change them out. Theoretically, you could do this for as many sub levels as you have.

happi posted

Hi, i've an issue who is similar to this tutorial, we have on the website www.glolighting.co.za some grouped products with two different attribute set, from two categories ID, cat ID1, cat ID2, the problem is the grouped products display on the filter both cat ID1 and ID2 but i'll like to display on my filter only the parent product cat ID1 for the grouped products, any help please.

Post a comment