How to determine which top level category is active - magento

On my catalog page I want to get the currently active top level category.
Example:
1. Top Level Category
1.1 Sub Category
1.1.1 Sub sub Category
1.1.2 Sub sub Category
1.2 Sub Category
2. Top Level Category
2.1 Sub Category
2.2 Sub Category
Let's say I am in sub sub category 1.1.1. How do I get to know that '1. Top Level Category' is the current top level category?
I tried the following but it didn't work for me:
<?php $_cat_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_cat_helper->getStoreCategories() ?>
<?php foreach ($_categories as $_category): ?>
<?php if ($this->isCategoryActive($_category)): ?>
<?php echo $_category->getName() ?> <br>
<?php endif; ?>
<?php endforeach; ?>
Please help :-)
Update
If I put $this->isCategoryActive($_category) into catalog/product/list.phtml I get the following error:
Invalid method Mage_Catalog_Block_Product_List::isCategoryActive(Array
(
[0] =>
)
)
Trace:
#0 .../default/template/catalog/product/list.phtml(53): Varien_Object->__call('isCategoryActiv...', Array)
#1 .../default/template/catalog/product/list.phtml(53): Mage_Catalog_Block_Product_List->isCategoryActive(NULL)

Get the current category and climb up the category tree. You want the level 2 category (level 1 is the catalog root). Try something like this:
$category = Mage::getModel('catalog/category')
->load(Mage::registry('current_category'));
while ($category->getLevel() != 2) {
$category = Mage::getModel('catalog/category')
->load($category->getParentId());
}

Figured it out:
<?php $_categories = $this->getStoreCategories();?>
<?php foreach($_categories as $_category): ?>
<?php if($this->isCategoryActive($_category)): ?>
<?php echo $_category->getName(); ?>
<?php endif; ?>
<?php endforeach; ?>
Make sure the block type is catalog/navigation

Related

Get category same as admin category positions position

I'm trying to change the order of specific categories using a php script. The script below runs fine, and I'm seeing the position change values in catalog_category_entity, but order is unaffected in the admin, and in the front.
Please use drag and drop from admin
And reindex than flush cache
are you using your script like this ?
<?php
$_categories = $this->getStoreCategories();
?>
<?php foreach ($_categories as $_category) : ?>
<li class="<?php if($_category->getId() == $this->getCurrentCategoryId()) echo "active"; ?>">
<?php echo $_category->getName(); ?>
</li>
<?php endforeach; ?>
if not use it like this.

Display all the Post Categories in Magento Fishpig

I've two Post Categories with two different layouts, But now both are displaying in the same view.phtml. I need to create a check in which category the post belongs and display the style accordingly.
By using below method, I can load a single category with ID 2.
<?php $test = Mage::getModel('wordpress/term')->load(2);?>
Is there any way to load all the post categories.?
Shyam is almost there. Here is a slightly cleaner version of the code:
<?php $categories = $post->getTermCollection('category') ?>
<?php if (count($categories) > 0): ?>
<?php foreach($categories as $category): ?>
<?php if ((int)$category->getId() === 1): ?>
// Category ID #1
<?php elseif ((int)$category->getId() === 2): ?>
// Category ID #2
<?php else: ?>
// All other categories
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
By this method, you can split posts according to category and display in the same view.phtml with different layouts, for adding different layouts paste your code inside the if($getCategory == cat_id) section as I mentioned below.
<?php $categories = $post->getTermCollection('category') ?>
<?php if (count($categories) > 0): ?>
<?php foreach($categories as $category): ?>
<?php
$getCategory = $this->escapeHtml($category->getId());
echo "Get cat: ".$getCategory;
if($getCategory == 2)
{
//your code here
}
if($getCategory == 3)
{
//your code here
}
<?php endforeach; ?>
<?php endif; ?>

sort collection of category by name

I have custom links in menu that shows categories in drop-down.
For that I made a file in catalog/navigation->mainmenu.phtml
(a custom file)
Now, I want to show categories after sorting it by name. please help me how can I sort collection of categories by name. I already set order of category in admin. But in front-end it show unsorted.
Code is:-
<?php $defaultcategory= Mage::app()->getStore()->getRootCategoryId();?>
<?php $mainchildren = Mage::getModel('catalog/category')->getCategories($defaultcategory);?>
<ul>
<?php foreach ($mainchildren as $subcategory) : ?> <?php // 2 level ?>
<?php if($subcategory->getIsActive()):?>
<li id="show_subcat" class="<?php if($i==1): echo 'first'; endif; ?>" >
<?php $childid=$subcategory->getId();?>
<?php $subchild = Mage::getModel('catalog/category')->getCategories($childid);?>
<?php foreach ($subchild as $subchildcategory) : ?>
<?php $path=$subchildcategory->getRequestPath()?>
<?php break;?>
<?php endforeach ?>
<a href="<?php echo $this->getUrl().$path; ?>">
<?php echo $name= $subcategory->getName().$subcategory->getEnable() ?>
</a>
</li>
<?php endif;?>
<?php endforeach; ?>
</ul>
You can try :
children = Mage::getModel('catalog/category')->getCategories($defaultcategory)
->addAttributeToSort('name', 'ASC');
or :
children = Mage::getModel('catalog/category')->getCategories($defaultcategory)
->setOrder('name','ASC);
$categories = Mage::helper('catalog/category');
$collection = $categories->getStoreCategories(false,true,false);
foreach($collection as $_category)
{
//Do something
echo $_category->getName();
}
use this code to get collection and follow this link for more detailIn magento - same code for getting all categories running well with sorted in localhost but not in web server

How to add submenu on hover effect in left navigation in magento?

I have vertnav/left.phtml file code,
<div class="vertnav-container">
<div class="">
<h4 class="no-display"><?php echo $this->__('Category Navigation:') ?></h4>
<?php $store_categories = $this->toLinearArray($this->getStoreCategories()) ?>
<?php if ($count = count($store_categories)): ?>
<ul id="vertnav">
<?php endif; ?>
<?php foreach ($store_categories as $i => $_category): ?><?php $class = array() ?>
<?php if ($count == 1): ?>
<?php $class[] = 'only' ?>
<?php elseif (! $i): ?>
<?php $class[] = 'first' ?>
<?php elseif ($i == $count-1): ?>
<?php $class[] = 'last' ?>
<?php if (isset($store_categories[$i+1]) && $this->isCategoryActive($store_categories[$i+1])) $class[] = 'prev'; ?>
<?php if (isset($store_categories[$i-1]) && $this->isCategoryActive($store_categories[$i-1])) $class[] = 'next'; ?>
<?php echo $this->drawOpenCategoryItem($_category, 0, $class) ?>
<?php endforeach ?>
<?php if ($count): ?>
</ul>
<?php endif; ?>
</div>
</div>
and set System > Configuration > Catalog > Category Vertical Navigation to 2 as per my requirement, but now on mouseover on that displayed category subcategories should be shown
so how can i do customization to that and add hover effect code to this?
Please help me
If you take a closer look at the drawOpenCategoryItem of the Mage_Catalog_Block_Navigation you might notice that the method does a check whether the given category is part of the current category path. So only when this check returns true, the children categories of this category will be rendered. For other categories the script will not go into that part of the code.
This sounds to be case if I understand your question correctly.
if (in_array($category->getId(), $this->getCurrentCategoryPath())){
$children = $category->getChildren();
$hasChildren = $children && $children->count();
if ($hasChildren) {
$htmlChildren = '';
foreach ($children as $child) {
$htmlChildren.= $this->drawOpenCategoryItem($child);
}
if (!empty($htmlChildren)) {
$html.= '<ul>'."\n"
.$htmlChildren
.'</ul>';
}
}
}
Additional to this information. The drawOpenCategoryItem() is never actually called upon in the whole PHP codebase.
So to have these rollover effects you need to have code that generates the full tree structure, or at least a big enough part of it. Regarding the System > Configuration > Catalog > Category Vertical Navigation. I guess you customized that yourself?
To give you a few pointers. You might want to have a look at the following methods. These are used for the rendering of the top menu and are actually doing the thing you are planning to implement.
Mage_Catalog_Block_Navigation::renderCategoriesMenuHtml()
Mage_Catalog_Block_Navigation::_renderCategoryMenuItemHtml()
Hopes this helps you getting things underway.

Magento Add filter by subcategory in tool bar

I'm trying to filter products listing with subcategory, Lets say i'm having a top level category named perfume and to subcategory named for him & for her. In top level category listing page i'm trying to include option named filter results it should show a dropdown as for men and for women
If user clicks for men it should show only products in for men category
To do this i tried with,
<?php $categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('my_attribute')
->addAttributeToSelect('position')
->addAttributeToSort('position', 'ASC')
->setLoadProductCount(true)
->addAttributeToFilter('is_active',array('eq'=>true))
->load(); ?><?php foreach($categories as $key=>$category): ?><?php if($category->getName() != ''):?> <?php $prodCollection = age::getResourceModel('catalog/product_collection')->addCategoryFilter($category); // Magento product collection ?>
<?php echo $category->getName() ?> (<?php echo $prodCollection->count() ?>)<br/>
But it's showing all categories with product counts.
Did anyone know how to filter this to display subcategories only to current main category?
Thanks,.
<?php
$rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootCategoryId);
?>
<?php foreach($categories as $category): ?>
<?php echo $category->getName() ?><br/>
<?php endforeach ?>

Resources