display category on left side bar in magento - magento

I just want to display category and subcategory on left side bar, i have a code that work according to my need but this code only display name of the category it didn't get url for a particular category name.
code :-
<?php
$_categories = Mage::getBlockSingleton('catalog/navigation');
foreach ($_categories->getStoreCategories() as $_category) {
$category = Mage::getModel('catalog/category');
$category->load($_category->getId());
$subcategories = explode(',', $category->getChildren());
?>
<?php
foreach ($subcategories as $subcategoryId) {
$category->load($subcategoryId);
echo '<li>' . $category->getName() . '</li>';
}
?>
<?php
}
?>
It only display name for each subcategory but can't linked it with particular url for each sub category properly. suggest me where i did mistake.

Instead of using category object you need to load the subcategory again ex:
<?php
foreach ($subcategories as $subcategoryId) {
//$category->load($subcategoryId); instead
$subcategory = Mage::getModel("catalog/category")->load($subcategoryId);
echo '<li>' . $subcategory->getName() . '</li>';
}
?>
Second and good method as follows :
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Please note that you may need to tweak some bit of code above.
Hope this helps.

Related

How to create Custom Menu in magento with active and deactive class for category?

I need a custom categories menu in magento. and i want a active class on active category. when i click on category then that category should be add active class.
(i) First of all create your custom category from admin panel
(ii)Then pass the category id in getCategories() function in code
Try this code,it works for me.I hope this work for you.
<?php
$children = Mage::getModel('catalog/category')->getCategories(2);
?>
<div class="menusecond">
<ul id="second menu">
<?php
foreach ($children as $category) { ?>
<li class="mymenu">
<?php $catId = $category->getId(); ?>
<?php $cat = Mage::getModel('catalog/category')->load($catId); ?>
<a href="<?php echo $cat->getUrl(); ?>">
<?php echo $cat->getName(); ?>
</a>
</li>
<?php
}
?>
</ul>
</div>
I found the solution. we can identify current category by category getUrlKey.
<?php
$_helper = Mage::helper('catalog/category');
$_categories = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('is_active', 1) //only active categories
->addAttributeToFilter('include_in_menu', 1);
$currentCategory = Mage::registry('current_category') ;
$rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
$rootCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
$childIds = explode(',',$rootCategory->getChildren()); ?>
<div class="row">
<?php if (count($childIds) > 0): ?>
<ul>
<?php foreach($childIds as $_category):
$_category = Mage::getModel('catalog/category')->load($_category); ?>
<?php if ($_category->getIncludeInMenu()) { ?>
<?php $allsubchild = explode(',',$_category->getChildren());?>
<?php foreach($allsubchild as $_sub): ?>
<?php endforeach; ?>
<li class="<?php if (strpos($_SERVER['REQUEST_URI'], $_category->getUrlKey()) !== false){ echo "active";} ?>">
<?php echo $_category->getName() ?>
<?php } ?>
<ul>
<?php $ss = 0; foreach($allsubchild as $_sub):
$_subcategory = Mage::getModel('catalog/category')->load($_sub);
?>
<?php if ($_subcategory->getIncludeInMenu()) { ?>
<?php
$uri = $_category->getUrlKey();
$uri .= "/";
$uri .= $_subcategory->getUrlKey(); // echo $uri; ?>
<li class="<?php if($_subcategory->getChildrenCount()) { echo "has-dropdown1"; }?> <?php if (strpos($_SERVER['REQUEST_URI'], $uri) !== false){ echo "active";} ?>">
<?php echo $_subcategory->getName(); ?>
<?php $suren = explode(',',$_subcategory->getChildren());?>
<ul>
<?php
foreach($suren as $suren1){
$suren11 = Mage::getModel('catalog/category')->load($suren1);
$_suren1id = $suren11->getId();
$internet_count = Mage::getModel('catalog/category')->load($_suren1id)->getProductCount();
if ($internet_count > 0) {
if ($suren11->getIncludeInMenu()) { ?>
<?php
$uri1 = $uri;
$uri1 .= "/";
$uri1 .= $suren11->getUrlKey(); // echo $uri1; ?>
<li>
<a class="<?php if (strpos($_SERVER['REQUEST_URI'], $uri1) !== false){ echo "active";} ?>" href="<?php echo $suren11->getUrl(); ?>" title="<?php echo $suren11->getName(); ?>"><?php echo $suren11->getName(); ?></a>
</li>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>
</li>
<?php }?>
<?php $ss++; endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
Goto app/design/frontend/rwd/default/template/page/html and open topmenu.phtml file. find this code <?php if($_menu): ?>
<nav id="nav">
<ol class="nav-primary">
<?php echo $_menu ?>
</ol>
</nav>
<?php endif ?>
this code will output you the categories in the form of top menu if you don't use any categories hide this code or else paste this code before or after this code.
<ul>
<li>HOME</li>
<li>ABOUT US</li>
<li>your_page_name</li>
<li>your_page_name</li>
</ul>

show active and inactive categories magento for parent level

I have a website in magento. i want show all categories whether it is active or not.
I have tried but it doesnt show inactive categories.
Can any give a suggestion to solve this problem ?
Thanks.
try this one... this will surely help you...
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')//or you can just add some attributes
->addAttributeToFilter('level', 2)//2 is actually the first level;
add this filter option if you want all active categories.
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')//or you can just add some attributes
->addAttributeToFilter('level', 2)//2 is actually the first level
->addAttributeToFilter('is_active', 1);//if you want only active categories
Please refer this link, may be help you,
http://fishpig.co.uk/magento/tutorials/display-categories-and-subcategories-in-magento/
Regards.
Here is the correct code:
<?php
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addLevelFilter(2);
$b_path = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
?>
<div class="nav-container">
<ul id="nav">
<?php
foreach($categories as $cat)
{
$category = Mage::getModel('catalog/category')->load($cat->getentity_id());
if($category ->getname() == 'Root Catalog' || $category ->getname() == 'Default Category' )
{}else{
?>
<li><?php echo $category ->getname(); ?></li>
<?php }} ?>
</ul>
</div>
Thanks
<?php
/*
* http://fishpig.co.uk - Magento Tutorials
*
* Display top level categories and subcategories
*
**/
?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

How to display all categories in magento project

am new to magento 1.7 and am using celebrity theme now what i want is to add all the categories to be displayed in left side bar , i have browsed through n tried many but none of them are working (May be due to Celebrity theme). So can any one help me on this ASAP. PLEASE
you can add like this
go to your layout.xml and add like this
/app/design/frontend/default/default/layout/catalog.xml
Open this Xml file and paste this code.
<reference name="left">
<block type="catalog/navigation" name="catalog.leftnav" template="catalog/navigation/left_nav.phtml" />
</reference>
open this file ..
/app/design/frontend/default/default/template/catalog/navigation/left_nav.phtml
paste this code:
<?php
$obj = new Mage_Catalog_Block_Navigation();
$store_cats = $obj->getStoreCategories();
$current_cat = $obj->getCurrentCategory();
$current_cat = (is_object($current_cat) ? $current_cat->getName() : '');
foreach ($store_cats as $cat) {
if ($cat->getName() == $current_cat) {
echo '<li class="current">'.$cat->getName()."\n<ul>\n";
foreach ($obj->getCurrentChildCategories() as $subcat) {
echo '<li>'.$subcat->getName()."</li>\n";
}
echo "</ul>\n</li>\n";
} else {
echo '<li>'.$cat->getName()."</li>\n";
}
}
?>
EDIT
LIST ALL CATEGORIES
$_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php //if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
<?php $_category1 = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>
<?php $_subcategories1 = $_category1->getChildrenCategories() ?>
<?php if (count($_subcategories1) > 0): ?>
<ul>
<?php foreach($_subcategories1 as $_subcategory1): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory1) ?>">
<?php echo $_subcategory1->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php // endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif;?>

How to show All (product) Categories List in admin side: Magento

I want to show All Product Categories in admin side of My module in System.xml as a multiselect.
$_category = Mage::getModel('catalog/category')->load();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
foreach($collection as $cat){
if($_category->getIsActive()){
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
$helper->getCategoryUrl($cat);
echo $cat->getName();
}
}
But it will not show what i am want, i want only product categories.... Can some one idea about it... Thanx.
To show Category selection in system configuration,i have find a solution for it by extending a Mage Model class and method.
Mage_Adminhtml_Model_System_Config_Source_Category
and remove the line.
->addPathFilter('^1/[0-9]+$')
Now it display multiselect option in system configuration. Where you can select multi categories from the list..
I was working on Magento 1.7 and I didn't see a line that contains
->addPathFilter('^1/[0-9]+$')
But, removing a following line worked for me.
->addRootLevelFilter()
?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo "<b>".$_category->getName(). $_category->getId()."</b>" ?>
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo "--".$_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Display a menu with categories, sub-categories and products - Magento

I have been looking to implement a menu system like the following
I have 1 category, Cars, with 2 sub-categories, New and Used
I’d like to display, on the drop down, the main category and then New, with all the products inside as a list and then Used, with all the products in a list.
I have tried using the code in the link provided, but it seemed to include a rollover option, that expanded the menu and the products are not listed below the category.
Thanks
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Source: http://fishpig.co.uk/magento-tutorials/display-categories-and-subcategories-in-magento

Resources