In this post we will see how to get layered navigation filters by category id programmatically anywhere you want.

Using this script you will be able to get :

1. Attribute code like Color
2. Attribute options or label like Black, Blue, Red
3. Options value like 20,27,28 (value for above Black, Blue, Red)

So we will make a php file in our root directory for this. Like magento/getLayerdByCat.php

$mageFilename = 'app/Mage.php';
require_once $mageFilename;


$categoryID = 4;
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();

    $data = array('status' => true);
    $data['data'] = array();
    $layer = Mage::getModel("catalog/layer");
    $category = Mage::getModel("catalog/category")->load($categoryID);
    $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());
            $data['data'][] = array('name'=>ucfirst($attribute->getAttributeCode()),'count'=>$count);
catch (Exception $e) {
    $data = array('status' => false, 'message' => $e->getMessage());
echo '<pre>'; print_r($data); die((__FILE__).'--'.(__FUNCTION__));
echo json_encode($data);

So you can access values from $data and display if you want to. May be you can return it like return echo json_encode($data) .

I hope it helps.

Be kind to one another. Take care!

No Comments

Post A Comment