Magento - get filterable attributes by category

19,573

Solution 1

The answer that Joe gave was a good starting point, but the attributes didn't returned any options yet. After a lot of frustrations I solved the problem with the following code. Hope it helps all of you out.

$layer = Mage::getModel("catalog/layer");
foreach($categories as $categoryid) {
    $category = Mage::getModel("catalog/category")->load($categoryid);
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();

    foreach ($attributes as $attribute) {
        if ($attribute->getAttributeCode() == 'price') {
            $filterBlockName = 'catalog/layer_filter_price';
        } elseif ($attribute->getBackendType() == 'decimal') {
            $filterBlockName = 'catalog/layer_filter_decimal';
        } else {
            $filterBlockName = 'catalog/layer_filter_attribute';
        }

        $result = $this->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();

        foreach($result->getItems() as $option) {
            echo $option->getLabel().'<br/>';
            echo $option->getValue();
        }
}

The only thing you'll need to do yourself is create the correct link using the getValue() functions.

This code has been tested in Magento 1.5

Solution 2

Magento uses the model Catalog_Model_Layer to accomplish this, so I'm guessing this may be your best bet. Caveat emptor, I have not tested this code yet:

$layer = Mage::getModel("catalog/layer");
foreach($categories as $categoryid) {
    $category = Mage::getModel("catalog/category")->load($categoryid);
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();
    // do something with your attributes
}

Each iteration here will give you an object of the class Mage_Catalog_Model_Resource_Eav_Mysql4_Attribute_Collection, which you should be able to iterate over in a foreach loop to get your desired output.

For caching, try enabling block caching on your site and give the block a cache tag like the following. Magento will cache the HTML output and all will be right with the world:

protected function _construct() {
    $this->addData(array(
        'cache_lifetime' => 3600,
        'cache_tags'     => array(Mage_Catalog_Model_Product::CACHE_TAG),
        'cache_key'      => $someUniqueIdentifierYouCreate,
    ));
}

The cache will only be valid for the key you pass, so make sure that, if the menu is to change (w/o flushing the cache, for instance), that the cache key is different.

Hope that helps!

Thanks, Joe

Share:
19,573
Neelima kuchipudi
Author by

Neelima kuchipudi

Updated on June 03, 2022

Comments

  • Neelima kuchipudi
    Neelima kuchipudi almost 2 years

    I have created a custom navigation module specifically for a website, but I really want to be able to list filterable attributes by a specific category. So for instance my main navigation is:

    • Category 1
    • Category 2
    • Category 3 etc.

    I then that when a user mouses over a category, they are then presented with an expanded menu with a few filterable options e.g.:


    Category 1

    View by manufacturer:

    • Manufacturer 1
    • Manufacturer 2
    • Manufacturer 3 etc.

    I am able to get all filterable attributes for the store, but I want this list to pull in only the filterable attributes per category, as for instance Category 1 may have different manufacturers to Category 2. I then need to cache these results as this will not change often.

  • Neelima kuchipudi
    Neelima kuchipudi almost 14 years
    Ah thanks Joseph, that's been really useful. There is still one thing that I can't seem to get right though, this code is for listing the available filterable attributes per category which works well, but my problem is that I cannot work out how to filter the attribute options by category. So for example, Category 1 & Category 2 may both use a 'Manufacturers' attribute, but they may have different options dependent on the category - e.g. Cat 1 could be TVs made by Sony & Panasonic, whereas Cat 2 could be phones made by Sony, Motorola and Apple.
  • Joe Mastey
    Joe Mastey almost 14 years
    Not sure on that one. Did you make sure to configure your attributes in the backend so that they only display w/ results?
  • Neelima kuchipudi
    Neelima kuchipudi almost 14 years
    Hey there - still no joy - the closes I have found is the options() method in Mage_Catalog_Model_Category_Attribute_Api, which looks as though it should work fine, but ->getAttribute doesn't seem to exist as a public method in Mage_Catalog_Model_Category. Any thoughts anyone?
  • mononym
    mononym about 12 years
    I'm not able to get the correct url (on the home page it emits the category), any help on building it?
  • mononym
    mononym about 12 years
    nevermind: <?php echo $this->getCategoryUrl($_category).'?'.$attribute->getAttribu‌​teCode().'='.$option‌​->getValue()?>
  • maljukan
    maljukan almost 11 years
    @Jasper @mononym How did you guys obtain $categories collection, i used $helper = Mage::helper ('catalog/category'); $categoriesCollection = $helper->getStoreCategories (); But now how to get single categories id's?
  • RPDeshaies
    RPDeshaies about 10 years
    Is it possible to add filter the a layer like that ? Like if previous filter where applied on the collection ?