Layered navigation filters for grouped products only
-
16-10-2019 - |
Question
Well, I'm having a category filled only with grouped products and I want these ones to be filtered by their associated ( and simple ) products's attributes.
Until now I've used a dirty workaround : I've left only one simple product to be visible to catalog but hidden by html & css.
That way the attributes were appearing and I could use them for the grouped products also, but I've noticed that layered navigation dissapears if that hidden simple product is not appearing when I filter by price.
Any ideas or workarounds?
Thanks.
Solution
You need to rewrite how Mage_Catalog_Model_Layer
builds the attribute collection using the current product collection's attribute sets:
protected function _getSetIds()
{
$key = $this->getStateKey().'_SET_IDS';
$setIds = $this->getAggregator()->getCacheData($key);
if ($setIds === null) {
$setIds = $this->getProductCollection()->getSetIds();
$this->getAggregator()->saveCacheData($setIds, $key, $this->getStateTags());
}
return $setIds;
}
For this context, you'll want to check the product collection for grouped products and return the attribute set IDs of grouped children. These are then evaluated in getFilterableAttributes()
:
public function getFilterableAttributes()
{
$setIds = $this->_getSetIds();
if (!$setIds) {
return array();
}
/** @var $collection Mage_Catalog_Model_Resource_Product_Attribute_Collection */
$collection = Mage::getResourceModel('catalog/product_attribute_collection');
$collection
->setItemObjectClass('catalog/resource_eav_attribute')
->setAttributeSetFilter($setIds)
->addStoreLabel(Mage::app()->getStore()->getId())
->setOrder('position', 'ASC');
$collection = $this->_prepareAttributeCollection($collection);
$collection->load();
return $collection;
}
You will likely need to deal with the cache key. I would develop with block caching off and then test extensively with it on.