pzirkind
pzirkind

Reputation: 2338

Magento: Showing sibling categories in layered navigation

I would like to add sibling categories to layered navigation (whenever a customer is is already one layer in).

In other words: lets say I have a category called 'Animals' and sub categories named 'Cats' 'Dogs' and 'Lions' respectively, if a customer clicks on 'Lions' I want them to see in "Shop by category" Cats and Dogs.

Does anyone have any idea on how to do this?

Thanks in advance.

Upvotes: 3

Views: 4941

Answers (4)

TheKitMurkit
TheKitMurkit

Reputation: 501

Method app/code/core/Mage/Catalog/Block/Navigation.php::getCurrentChildCategories() does what is needed and keeps product counts correct. You can copy it's functionality to your own helper.

Upvotes: 0

Rehan Mobin
Rehan Mobin

Reputation: 552

Go to

app/code/core/Mage/Catalog/Model/Layer/Filter/Category.php

find function _getItemsData()

see line #163:

$categories = $categoty->getChildrenCategories();

remove this line and paste these

if(count($categoty->getChildrenCategories())){
    $categories = $categoty->getChildrenCategories();
}else{
    $categories = $categoty->getParentCategory()->getChildrenCategories();
}

Upvotes: 1

CCBlackburn
CCBlackburn

Reputation: 1704

This will retrieve the id, url and name of the sibling categories

$currentCategory = $this->helper('catalog/data')->getCategory();
$parentCategoryId = $currentCategory->parent_id;

$siblingCategoryIds = array();
$siblingCategories = array();

foreach(Mage::getModel('catalog/category')->load($parentCategoryId)->getChildrenCategories() as $siblingCategory) {
    if ($siblingCategory->getIsActive()) {
        $siblingCategories[] = array($siblingCategory->getId(), array('name' => $siblingCategory->getName(), 'url' => $siblingCategory->getUrl()));
        $siblingCategoryIds[] = $siblingCategory->getId();
    }
}

$pos = array_search($currentCategory->getId(), $siblingCategoryIds);

$nextPos = $pos+1;
$prevPos = $pos-1;

if (isset($siblingCategoryIds[$nextPos])) {
    $nextCategory = $siblingCategories[$nextPos];
}
else {
   $nextCategory = null;
}

if (isset($siblingCategoryIds[$prevPos])) {
    $prevCategory = $siblingCategories[$prevPos];
}
else {
   $prevCategory = null;
}

echo var_dump($prevCategory);
echo var_dump($nextCategory);

Upvotes: 0

Magento Guy
Magento Guy

Reputation: 2493

This shouldn't be too difficult. The code below is untested, but it should work wherever you put it in the front end. What it will do is give you access to the collection of sibiling categories, but you will need to figure out where in the templates you put it.

$parentId = Mage::registry('current_category')->getParentCategory()->getId();
$cats = Mage::getModel('catalog/category')->load($parentId)->getChildrenCategories();
foreach ($cats as $cat) {
  // first, skip current category.
  if ($cat->getId() == Mage::registry('current_category')->getId()) continue;
  // then do something with $cat
  echo $cat->getName().", ";
}

Upvotes: 7

Related Questions