Magento: Display Filterable Attributes by Category
In Magento, you can easily display filterable attributes for an entire store. However, if you want to restrict these attributes based on the selected category, you’ll need to combine data from both categories and attribute models. Here’s a step-by-step guide to achieve this.
Getting Started
First, let’s understand what we’re trying to accomplish:
- We have multiple categories in our navigation menu (e.g., Category 1, Category 2).
- When a user hovers over a category, we want to display the filterable attributes specific to that category.
- The list of manufacturers should be different for each category since they are filterable attributes.
- We need to cache these results as they won’t change frequently.
Retrieving Filterable Attributes
To get all filterable attributes from the store, you can use the following code:
$filterAttributes = Mage::getResourceModel('catalog/product_attribute_collection')
->addFieldToFilter('is_filterable', 1)
->load();
This retrieves a collection of all product attributes flagged as is_filterable. However, this doesn’t help us filter by category.
Filtering Attributes by Category
Since we want to filter the list based on the currently selected category, we need to combine data from both categories and attribute models. Here’s an approach using Magento’s category collection:
- Get the current category ID.
- Retrieve all products in that category.
- Extract unique attributes used in those products.
Let’s break it down:
Step 1: Getting the Current Category ID
You can retrieve the current category ID from the Mage::registry object, which is usually set after a successful category selection. The specific method depends on how your navigation is implemented.
For example, if you’re using the core Magento layered navigation, the category ID is stored in the registry as current_category.
Step 2: Retrieve Products in the Category
Once you have the category ID, you can get all products associated with that category:
$category = Mage::getModel('catalog/category')->load($categoryId);
$products = $category->getProductCollection();
Step 3: Extract Unique Attributes
Now we need to extract unique attributes from these products. You can achieve this by looping through the product collection and adding unique attribute IDs to a set:
$attributeIds = [];
foreach ($products as $product) {
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getIsFilterable()) {
$attributeIds[] = $attribute->getAttributeId();
}
}
}
This code loop through each product in the category, retrieves its attributes, and adds any filterable attribute IDs to the $attributeIds array. The set() function ensures we only keep unique IDs.
Caching the Results
To cache these results, you can use Magento’s built-in caching system. Here’s a simple example:
$cacheKey = 'category_filter_attributes_' . $categoryId;
$cachedData = Mage::app()->getCache()->load($cacheKey);
if (!$cachedData) {
// Cache data is not available, so generate and cache it
// ...
// After generating the data, save it to the cache:
Mage::app()->getCache()->save(serialize($filteredAttributes), $cacheKey, array('category_filter_attributes'));
} else {
// Use cached data
$filteredAttributes = unserialize($cachedData);
}
Conclusion
Displaying filterable attributes based on a specific category in Magento requires combining data from both categories and attribute models. By using the approach outlined aboveâretrieving products from the category, extracting unique filterable attributes, and caching the resultsâyou can create an efficient system that meets your requirements.
Remember to clear the cache or regenerate the cached data whenever you add or update attributes in the store.
For more advanced Magento development techniques, consider exploring the resources at LaravelCompany.
Happy coding!