Get a category in magento - magento

I have this kind of categories in magenta:
-MotoCross
->Parts1 ->sub-parts
->Parts2 ->sub-parts
->Parts3 ->sub-parts
-Quad
->Parts1 ->sub-parts
->Parts2 ->sub-parts
->Parts3 ->sub-parts
For now i have MotoCross and Quad printed on screen and their child (parts1,2,3) but I would like to have Part1 and them sub parts printed on screen.
I don't know how to do, see my code bellow:
---------------------------EDIT---------------------------
Now I have the current category categories, but i don't know how two have current category categories categories. You're still here ? :P
<?php
$object = new Mage_Catalog_Block_Navigation();
$actualCategoryId = $object->getCurrentCategory()->getId();
$actualCategory = Mage::getModel('catalog/category')->load($actualCategoryId);
$subCategories = explode(',', $actualCategory->getChildren());
?>
<div id="categories_list">
<ol>
<?php
foreach ( $subCategories as $subCategoryId )
{
$category = Mage::getModel('catalog/category')->load($subCategoryId);
if ( $category->getIsActive() )
{ ?>
<li class="second_level_cat">
<?php
echo ''.$category->getName().' ';
?>
</li>
<?php
}
}
?>
</ol>
</div>
Thanks for your help.

It's something like this.
<div id="categories_list">
<ol>
<?PHP foreach ($catIds as $cid): ?>
<?PHP $_category = Mage::getModel('catalog/category')->load($cid); ?>
<?PHP if($_category->getLevel() == 2 ) : ?>
<li class="second_level_cat">
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a>
</li>
<?php
$cat = Mage::getModel('catalog/category')->load($_category->getId());
$childrens = $cat->getChildren();
if($childrens)
{
$childs=explode(",",$childrens);
foreach($childs as $child){
$cat_child = Mage::getModel('catalog/category')->load($child);
?>
<li class="third_level_cat">
<a href="<?php echo $this->getCategoryUrl($cat_child) ?>"<?php if ($this->isCategoryActive($cat_child)): ?> class="current"<?php endif; ?>><?php echo ucfirst(strtolower($this->htmlEscape($cat_child->getName()))) ?></a>
</li>
<?php
}
}
?>
<?php endif; ?>
<?php endforeach ?>
</ol>
</div>

Here is code
$cats = Mage::getModel('catalog/category')->load(2)->getAllChildren(); // 2 = Root Category
$catIds = explode(',',$cats);
Select Category :
<select id="_cid" name="_cid">
<?PHP foreach ($catIds as $cid){ ?>
<?PHP $_category = Mage::getModel('catalog/category')->load($cid); ?>
<?PHP if($_category->getLevel() == 3 ) { ?>
<option value="<?PHP echo $_category->getId() ?>"> <?PHP echo $_category->getName() ?></option>
<?PHP } ?>
<?PHP if($_category->getLevel() == 4 ) { ?>
<option value="<?PHP echo $_category->getId() ?>"> <?PHP echo $_category->getName() ?></option>
<?PHP } ?>
<?PHP } ?>
</select>

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>

display category on left side bar in 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.

count subcategory of current category in magento

magento,in if statement im using a condition for two option, first one is current category has zero sub category and second one is current category has more than zero subcategory, but this condition is not working with this code
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php if (count($categories) > 0): ?>
<?php $_filters = $this->getFilters() ?>
<?php foreach ($_filters as $_filter): ?>
<?php if($_filter->getName() == 'Brand' || $_filter->getName() == 'Category' || $_filter->getName() == 'Store' || $_filter->getName() == 'Price' ): ?>
<?php if($_filter->getItemsCount()): ?>
<dt><div style="background-color:#cdcdcd;padding:4px;border-radius:3px;"><?php echo $this->__($_filter->getName()) ?></div></dt>
<dd><div style="max-height:150px;overflow:auto;padding-top:5px;"><?php echo $_filter->getHtml() ?></div></dd>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php else : ?>
<?php $_filters = $this->getFilters() ?>
<?php foreach ($_filters as $_filter): ?>
<?php if($_filter->getItemsCount()): ?>
<dt>
<div style="background-color:#cdcdcd;padding:4px;border-radius:3px;"><?php echo $this->__($_filter->getName()) ?></div>
<?php if ($removeUrl = Mage::helper('layerednavigation')->getRemoveUrl($_filter)): ?>
<a class="btn-remove" href="<?php echo $removeUrl ?>" title="<?php echo $this->__('Clear All') ?>"><?php echo $this->__('Clear All') ?></a>
<?php endif ?>
</dt>
<dd><div style="max-height:150px;overflow:auto;padding-top:5px;"><?php echo $_filter->getHtml() ?></div></dd>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
</dl>
<script type="text/javascript">decorateDataList('narrow-by-list')</script>
<?php endif; ?>
ckeck its first statement and give me solution
Hello check below code may be help you
<?php
$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
$children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
$countSubcategory=0;
foreach ($children as $category)
{
$countSubcategory++;
}
echo $countSubcategory;
?>

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;?>

Magento: Can't get categories children

For some reason this code is returning NULL when trying to get a categories subcategories.
<?php var_dump($_category->getChildrenCategories()); ?>
Here is the full code from a .phmtl file.
<ul id="nav_vert">
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php if ($_category->getIsActive()) { ?>
<?php $open = $this->isCategoryActive($_category); ?>
<?php $potential = $_category->hasChildren(); ?>
<li><a href="<?php echo $this->getCategoryUrl($_category); ?>"<?php if($open) { echo ' class="open"'; } ?><?php if($potential) { echo ' class="potential"'; } ?> ><?php if($potential&&$open) { echo 'v '; } elseif($potential) { echo '> '; }else{ echo ' '; }?><?php echo $_category->getName();?></a>
<?php if ($open && $potential): ?>
<?php var_dump($_category->getChildrenCategories()); ?>
<ul>
<?php foreach ($_category->getChildrenCategories() as $subcategory): ?>
<?php $subCat = Mage::getModel('catalog/category')->load($subcategory); ?>
<?php $open = $this->isCategoryActive($subCat); ?>
<?php $potential = $subCat->hasChildren(); ?>
<li><a href="<?php echo $this->getCategoryUrl($subCat); ?>"<?php if($open) { echo ' class="subopen"'; } ?><?php if($potential) { echo ' class="potential"'; } ?><?php if(!$potential&&$open) { echo ' class="final"'; } ?> ><?php if($potential&&$open) { echo ':: '; } elseif($potential) { echo '> '; }?><?php echo $subCat->getName(); ?> (<?php echo $subCat->getProductCount(); ?>)</a>
<?php if ($open && $potential): ?>
<ul>
<?php foreach ($subcategory->getChildrenCategories() as $subsubcategory): ?>
<?php $subsubCat = Mage::getModel('catalog/category')->load($subsubcategory); ?>
<?php $open = $this->isCategoryActive($subsubCat) ?>
<li><a href="<?php echo $this->getCategoryUrl($subsubCat); ?>" <?php if($open) { echo ' class="final"'; } ?>><?php echo $subsubCat->getName(); ?> (<?php echo $subsubCat->getProductCount(); ?>)</a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php } ?>
<?php endforeach ?>
</ul>
This line always returns true
<?php $potential = $_category->hasChildren(); ?>
And I know that the category has children.
Can anyone suggest why this doesn't work?
This is how I place the phtml in the page:
<reference name="left">
<block type="catalog/navigation" name="catalog.vertnav" template="catalog/navigation/vert_nav.phtml" before="-" />
</reference>
Magento version 1.5.1.0
Try using $_category->getChildren() (instead of $_category->getChildrenCategories() )
Have an easy day,
Pesach
You might also like to try this,
foreach ($_category->getCategories($_category) as $subcategory):
getCategories() is a more complex form but it allows you more control too. By specifying a recursion level you can retrieve not just the children but grand-children too...
Try with this code,
$this->getCurrentCategory()->getChildrenCategories()
Hope this helps
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php foreach($categories as $category): ?>
<?php $subcategories = $category->getChildren() ?>
<?php foreach($subcategories as $subcategory): ?>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php foreach($subsubcategories as $subsubcategory): ?>
<?php endforeach; ?><!-- end foreach subsubcategories -->
<?php endforeach; ?><!-- end foreach subcategories -->
<?php endforeach; ?><!-- end foreach categories -->
This is the basic idea behind extracting children categories.
So accordingly you can work on it.

Resources