Magento - get filterable attributes by category

前端 未结 2 1114
礼貌的吻别
礼貌的吻别 2021-02-04 10:53

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

2条回答
  •  误落风尘
    2021-02-04 11:31

    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

提交回复
热议问题