josephtikva1
josephtikva1

Reputation: 789

Categories for layered navigation in Magento

I'm looking to enhance the layered navigation in Magento.

Presently, attributes that are used in layered navigation can't be grouped, meaning if you have several attributes that are logically in one group (i.e. attributes "height", "width" & "depth" which are "Dimensions", and "color" and "texture" belong in an "Appearance" section).

I think this would enhance the usability and navigation for users.

Before I go ahead and begin developing a module for this, I was wondering if anyone came across something like this for magento, and if not, do you have any tips how this should be done?

Joseph

Upvotes: 1

Views: 7185

Answers (2)

ben
ben

Reputation: 1936

I created a module for this. Here are the changes I made:

MyName/Navigation/Catalog/Model/Layer.php:

class MyName_Navigation_Catalog_Model_Layer extends Mage_Catalog_Model_Layer {
    public function getFilterableAttributes()
    {
        $setIds = $this->_getSetIds();
        if (!$setIds) {
            return array();
        }

        $collection = Mage::getResourceModel('catalog/product_attribute_collection')
            ->setItemObjectClass('catalog/resource_eav_attribute');

        $collection->addSetInfo(true);

        $collection->getSelect()->distinct(true);
        $collection
            ->setAttributeSetFilter($setIds)
            ->addStoreLabel(Mage::app()->getStore()->getId())
            ->setOrder('position', 'ASC');

        $collection = $this->_prepareAttributeCollection($collection);
        $collection->load();

        return $collection;
    }
}

I'm just rewriting the overridden function from Mage_Catalog_Model_Layer with that addition of the line:

        $collection->addSetInfo(true);

This ensures that the group data will be loaded when I need it.

The next two changes just allow you to access the data.

MyName/Navigation/Catalog/Model/Layer/Attribute.php:

class MyName_Navigation_Catalog_Model_Layer_Filter_Attribute extends Mage_Catalog_Model_Layer_Filter_Attribute {

    public function getGroupName($setId = 4) {       
        $attribute = $this->getAttributeModel();
        $group_id = $attribute->getData('attribute_set_info/' . $setId . '/group_id');
        $group = Mage::getModel('eav/entity_attribute_group')->load($group_id);
        $group_name = $group->getData('attribute_group_name');

        return $group_name;
    }

}

MyName/Navigation/Catalog/Model/Layer/Item.php:

class MyName_Navigation_Catalog_Model_Layer_Filter_Item extends Mage_Catalog_Model_Layer_Filter_Item {
    public function getGroupName()
    {
        return $this->getFilter()->getGroupName();
    }
}

MyName/Navigation/Catalog/Block/Layer/Filter/Attribute.php:

class MyName_Navigation_Catalog_Block_Layer_Filter_Attribute extends Mage_Catalog_Block_Layer_Filter_Attribute {
    public function getGroupName() {
        return $this->_filter->getGroupName();
    }
}

Tell magento to use my module and not the core files. MyName/Navigation/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>

<config>
    <modules>
        <MyName_Navigation>
            <version>0.1.0</version>
        </MyName_Navigation>
    </modules>
    <global>
        <blocks>
            <catalog>
                <rewrite>
                    <layer_filter_attribute>MyName_Navigation_Catalog_Block_Layer_Filter_Attribute</layer_filter_attribute>
                </rewrite>
            </catalog>
        </blocks>
        <models>
            <catalog>
                <rewrite>
                    <layer>MyName_Navigation_Catalog_Model_Layer</layer>
                    <layer_filter_attribute>MyName_Navigation_Catalog_Model_Layer_Filter_Attribute</layer_filter_attribute>
                    <layer_filter_item>MyName_Navigation_Catalog_Model_Layer_Filter_Item</layer_filter_item>
                </rewrite>
            </catalog>
        </models>
    </global>
</config>

Now you can call

$_item->getGroupName();

from your template file: template/catalog/layer/filter.php or

$_filter->getGroupName(); from your template file: template/catalog/layer/view.php and Group/Sort the attributes from there.

Upvotes: 5

ʍǝɥʇɐɯ
ʍǝɥʇɐɯ

Reputation: 4022

The code for the filtered navigation has been on the Magento forums for a long time, it still works in the most recent versions:

http://www.magentocommerce.com/boards/viewthread/5500/

This may provide what you need to customise the appearance of the filtered navigation to suit your needs.

You can also define in your attribute the sort order in the layered navigation. Rather than use '1, 2, 3' go for '100, 200, 300' so that later on you can define - say - 'width' to 210, etc. and slot the attributes in to the sort order you need.

Upvotes: 0

Related Questions