Magento - get filterable attributes by category

前端 未结 2 1119
礼貌的吻别
礼貌的吻别 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:35

    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().'
    '; 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

提交回复
热议问题