Magento Coding Solutions

how to get filterable attributes and options in magento programmatically

Add this code in Product List.phtml otherwise you should be assign category Id 
<?php
echo "<pre>";
$_category = Mage::registry('current_category');
$category_id= $_category->getId();
$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());
echo "</br>";
if($count > 0){
echo $attribute->getFrontendLabel();
echo $attribute->getAttributeCode();
}
if($count > 0){

foreach($result->getItems() as $option) {


echo '<a href="'.$this->urlEscape($option->getUrl()).'">'.$option->getLabel().'</a>';
echo " ";
//echo $option->getValue();

echo "(".$option->getCount().")";
}
}
}
echo "</pre>";
?>

No comments:

Post a Comment