Question

I wanted Category's Filterable Attributes and their options so i had used following code

$layer = Mage::getModel("catalog/layer");
$category = Mage::getModel("catalog/category")->load($category_id);
$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 = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
    $count = count($result->getItems());
    if($count > 0){
        $attribute->getFrontendLabel();
        $attribute->getAttributeCode();
    }
    if($count > 0){
        foreach($result->getItems() as $option) {
            $option->getLabel();
            $option->getValue();
        }
    }
}

now i am getting proper output. for example i am getting following output

brand - HP,Dell
processer - i3,i5,i7

now issue is when i apply filter called processor i7 i have only one product called dell so one dell product is displayed properly but i need filterable attributes as follows

brand - Dell
processer - i3,i5,i7

what i am getting is both HP and Dell.This is programmatically i am doing.I need the way layered navigation works in magento category listing page.

Was it helpful?

Solution

i did myself with the following code and just sharing the code which may be helpful to others here

$categoryId = '5'; // replace with your category id here
$category = Mage::getModel("catalog/category")->load($categoryId);
$layer = Mage::getModel("catalog/layer");
$layer->setCurrentCategory($category);

$setIds = Mage::getModel('eav/entity_attribute_set')
            ->load($attrSetName, 'attribute_set_name')
            ->getAttributeSetId();

$attributes = Mage::getResourceModel('catalog/product_attribute_collection');
$attributes->setItemObjectClass('catalog/resource_eav_attribute')
                ->setAttributeSetFilter($setIds)
                ->addStoreLabel(Mage::app()->getStore()->getId())
                ->setOrder('position', 'ASC');


$attributes->addFieldToFilter('additional_table.is_filterable', array('gt' => 0));
$attributes->load();

foreach ($attributes as $attribute) {
    $filter_attr = array();
    $filter_attr['title'] = $attribute->getFrontendLabel();
    $filter_attr['code'] = $attribute->getAttributeCode();

    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 =  Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
    $i=0;

    foreach($result->getItems() as $option) {
        $attr_option = array();
        if($attribute->getAttributeCode() == 'price') {
            $attr_option['label'] = str_replace(array('<span class="price">','</span>'),'',$option->getLabel());
        } else {
            $attr_option['label'] = $option->getLabel();
        }

        $attr_option['value'] = $option->getValue();
        $attr_option['count'] = $option->getCount();
        $i++;
        $filter_attr['options'][] = $attr_option;
    }

    if($i!=0){
        $filter_attributes[] = $filter_attr;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top