Pages

13 September, 2017

How to get layered navigation filter options/attributes and value in magento programmatically?

If you want to get layered navigation filter attributes or options by specific category ID then create following script :-

<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app();
ini_set('display_errors', 1);
try{
    $json = array('status' => true);
    $json['data'] = array();
    $layer = Mage::getModel("catalog/layer");
    $category = Mage::getModel("catalog/category")->load(9); // your category ID
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();
    foreach ($attributes as $attribute) {
        $filterBlockName = 'catalog/layer_filter_attribute';
        $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
        foreach($result->getItems() as $option) {
            $count[] = array('attribute_name' => $option->getLabel(),'attribute_value' => $option->getValue());
        }
        if($count!=null){
            $json['data'][] = array('name'=>ucfirst($attribute->getAttributeCode()),'count'=>$count);
        }
        unset($count);
    }
}
catch (Exception $e) {
    $json = array('status' => false, 'message' => $e->getMessage());
}
echo '<pre>';print_r($json);die;
//echo json_encode($json);
?>

Above script will gives you filter attributes for specific category in magento.

Hope it helps :)