如何通过 magento 的自定义模块获取产品列表过滤器?

How to get Product listing filters through custom module of magento?

我想使用我们自己的自定义 API 在类别列表页面中创建产品过滤器我已经尝试了很多方法,例如

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

但这只会加载该类别的可过滤属性代码,但我们需要该类别中可用的产品集合的过滤器值和计数。

下面的代码将加载该属性的所有可用选项,但我希望它用于特定的产品系列

foreach($attributes as $attribute){
    $options = $attribute->getSource()->getAllOptions(false);
}

就像我们的手机类别有 1000 部手机并具有可过滤属性品牌,所以我需要

Brand
    Brand A (100 products)
    Brand B (500 products)
    Brand C (400 products)

如果我不想加载整个产品集合,这怎么能实现,因为加载 1000 多个产品将花费太多时间来响应。

这段代码对我有用:

public function ws_getfiter($store_id,$categoryid){
                Mage::app()->setCurrentStore($store_id);        
                $layer = Mage::getModel("catalog/layer");

                $category = Mage::getModel("catalog/category")->load($categoryid);
                $layer->setCurrentCategory($category);
                $attributes = $layer->getFilterableAttributes();
                $filter = array();
                $count = 0;
                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';
                    }
                    $filter[$count]["code"] = $attribute->attribute_code;
                    $filter[$count]["type"] =  $attribute->frontend_input;
                    $filter[$count]["label"] =  $attribute->frontend_label;
                    $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
                    $innercount = 0;
                    $filter_data = array();
                    foreach($result->getItems() as $option) {
                        $filter_data[$innercount]["count"] =  $option->getCount();
                        $filter_data[$innercount]["label"] =  $option->getLabel();
                        $filter_data[$innercount]["id"] =  $option->getValue();
                        $innercount++;
                    }
                    $filter[$count]["values"] = $filter_data;
                    $count++;
                }
                return $filter;
            }

但是使用这个我们只能得到单级过滤器,但我想要它用于多级。