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

时间:2015-05-13 08:49:11

标签: magento

我想使用我们自己的自定义apis在类别的列表页面中创建产品过滤器我尝试过很多东西,比如

$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个产品需要花费太多时间来响应,这怎么能实现呢。

1 个答案:

答案 0 :(得分:0)

此代码适用于我:

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;
            }

但是使用这个我们只获得单级过滤器,但我希望它在多个级别。