Reputation: 235
Hi i want to create a Magento navigation which shall be visible on the category pages. It is a little confusing.
1. I have a parent category Designer3.
2. It has three sub categories CAT1, CAT2 and CAT3.
3. Now, CAT1 has three subcats: subcat1, sucat2 and subcat3.
Now, when i goto CAT1 page, i want CAT1 CAT2 and CAT3 to be all visible with their subcats in the navigation menu.
Depending upon the current category selected (CAT1, CAT2 or CAT3):
--> if current category is CAT1, CAT1 with its subcats will be displayed first.
--> If current cat is CAT2, navigation will be thus::
CAT2 with its subcats, CAT1 with its subcats and CAT3 with its subcats.
Further, if we select subcategries within CAT1, the position of Nav should be CAT1 - with subcats, CAT2 with subcats and CAT3 with subcats and the CAT1 subcat that is current category now should be highlighted.
Hope this makes sense.
I tried a number of things... but im very challenged when it comes to coding...so
This is what i tired.. this gives me the current category and its sub cats but when a sub cat is clicked it all goes for a toss.... any help and suggestion will be aprrecited... im a little challenged when it comes to magento...
<p><a href="<?php $currentCategory = Mage::registry('current_category');
//Display current category url
echo $this->getCurrentCategory()->getUrl();?>" class="ctActiveTitle">
<?php
$currentCategory = Mage::registry('current_category');
//Display current category
echo $this->getCurrentCategory()->getName();?></a>
<!--SHOWING SUB CATEGORIES OF CURRENT CATEGORY-->
<?php
$obj = new Mage_Catalog_Block_Navigation();// create obj
$cat = Mage::getModel('catalog/category')->load($obj->getCurrentCategory()- >getId()); //get current cat
$subcats = $cat->getChildren(); // find them kids
// loop on it
foreach(explode(',',$subcats) as $subCatid){ // split up the mage data for use
$_category = Mage::getModel('catalog/category')->load($subCatid);
$caturl = $_category->getURL();
$catname = $_category->getName();
// echo a link
echo '<a href="'.$caturl.'" class="ctBrownLink"> '.$catname.'</a>';
}?> </p> <p>Tnaks Moody</p>
Upvotes: 0
Views: 2241
Reputation: 1068
You don't really need to write custom code for this. The block Mage_Catalog_Block_Navigation
already contains all this logic and will solve most of your problems. At least the problem you describe with selected as sub-subcategory. See renderCategoriesMenuHtml()
for more information.
/**
* Render categories menu in HTML
*
* @param int Level number for list item class to start from
* @param string Extra class of outermost list items
* @param string If specified wraps children list in div with this class
* @return string
*/
public function renderCategoriesMenuHtml($level = 0, $outermostItemClass = '', $childrenWrapClass = '')
{
$activeCategories = array();
foreach ($this->getStoreCategories() as $child) {
if ($child->getIsActive()) {
$activeCategories[] = $child;
}
}
$activeCategoriesCount = count($activeCategories);
$hasActiveCategoriesCount = ($activeCategoriesCount > 0);
if (!$hasActiveCategoriesCount) {
return '';
}
$html = '';
$j = 0;
foreach ($activeCategories as $category) {
$html .= $this->_renderCategoryMenuItemHtml(
$category,
$level,
($j == $activeCategoriesCount - 1),
($j == 0),
true,
$outermostItemClass,
$childrenWrapClass,
true
);
$j++;
}
return $html;
}
Furthermore this method also automatically takes care of the differences you will encounter when swapping between non-flat and flat category and things like taking into account whether a category should be displayed or not.
Regarding your bit about the order of the navigation. From an usability point of view you might want to keep the order intact after selected a category. People probably might expect the navigation, or parts of them, on a specific place. Juggling them around isn't optimal, you make people search for things since the expected location will differ.
Upvotes: 1