I am doing a task of adding filterable attributes and their options to the category navigation menu.for this i got Marius article.Thanks to Marius.Now everything is done except i need one more attribute as By Category which will have the sub-category of the parent Category.
for example.
Mobiles
- Feature Phones
- Smart Phones
till now my work results following result
Mobile
- Feature Phone
- Smart Phone
- By Brand
- Apple
- Nokia
- Samsung
- LG
- By Operating System
- Android
- Windows
- Linux
What i need is
Mobile
- By Category
- Feature Phone
- Sub Category 1(if any)
- Sub Category 2(if any)
- Smart Phone
- By Brand
- Apple
- Nokia
- Samsung
- LG
- By Operating System
- Android
- Windows
- Linux
My code is
public function addAttributes($observer){
$menu = $observer->getMenu();
$tree = $menu->getTree();
foreach($menu->getChildren() as $child){
$nodeId = $child->getId();
if(substr($nodeId, 0,strlen('category-node-')) == 'category-node-'){
$id = str_replace('category-node-', '', $nodeId);
$mainUrl = $child->getUrl();
//echo $mainUrl; exit;
$attributes = $this->getAttributes($id);
foreach($attributes as $attribute){
//echo $attribute->getAttributeCode();
//mobile_bycategory
$cat_options = array();
if($attribute->getAttributeCode() =='mobile_bycategory'){
$children = Mage::getModel('catalog/category')->getCategories($id);
$i = 0;
foreach($children as $category){
$cat_options[$i]['value'] = $category->getId();
$cat_options[$i]['label'] = $category->getName();
$cat_options[$i]['url'] = $category->getRequestPath();
$i++;
}
//echo '<pre>'; print_r($options); exit;
}
if($attribute->getAttributeCode() =='mobile_bycategory'){
$options = $cat_options;
}else{
$options = $attribute->getSource()->getAllOptions();
}
//echo '<pre>'; print_r($options); exit;
if (count($options) > 0) {
$attrNodeId = 'attribute-'.$id.'-'.$attribute->getId();
$data = array(
'name' => Mage::helper('neo_topnav')->__('By %s', $attribute->getFrontendLabel()),
'id' => $attrNodeId,
'url' => '#',
'is_active' => false
);
$attrNode = new Varien_Data_Tree_Node($data, 'id', $tree, $menu);
//for each option add a new sub menu
foreach ($options as $option) {
if ($option['value']) {
$optionNodeId = 'attribute-'.$id.'-'.$attribute->getId().'-'.$option['value'];
$base_url = Mage::getBaseUrl();
if($attribute->getAttributeCode() =='mobile_bycategory'){ $final_url = $base_url.$option['url']; } else { $final_url = $mainUrl.'?'.$attribute->getAttributeCode().'='.$option['value']; }
$data = array(
'name' => $option['label'],
'id' => $optionNodeId,
'url' => $final_url,
'is_active' => false
);
$optionNode = new Varien_Data_Tree_Node($data, 'id', $tree, $menu);
$attrNode->addChild($optionNode);
}
}
$child->addChild($attrNode);
}
}
}
}
}
I had succefully done until
Mobiles
- By Category
- Feature Phone
- Smart Phone
I was stock on the Sub catogry of Subcatogy (if any)
Mobile
- By Category
- Feature Phone
- Sub Category 1(if any)
- Sub Category 2(if any)
- Smart Phone
Please Help Me.
Best Answer
Based on the solution provided by Marius and some code I had locally i put the below together.
Since the categories are added via an observer we simply disable the default observer and create own own version based on the original that adds the new filters in and pops the default categories under a "By Category" node when the Category level is 2.
}