I created a code to filter the product collection by categories. Making its own left side block using the following code.
I have it to work perfect for filtering, I am just having a hard time rendering the display.
The Level 2 categories work fine and only show
- Top Level Category 1
- Top Level Category 2
Once selected it then shows sub categories and their sub categories..
- Sub Category of 1
- Sub sub category of ^^^
- Sub sub category of ^^^
- Sub sub category of ^^^
- Sub sub sub category of ^^^
- Sub Category of 1
- Sub sub category of ^^^
- Sub sub category of ^^^
- Sub sub category of ^^^
- Sub sub sub category of ^^^
I need it to break down like the first level did. What would be the best method?
<?php
$root_category_id = Mage::app()->getStore()->getRootCategoryId();
$filterCategoryId = Mage::app()->getRequest()->getParam('cat');
$products = Mage::getSingleton('catalogsearch/advanced')->getProductCollection();
$catIds = array();
if (isset($products)) foreach ($products as $product) {
if ($product->isSaleable()) {
$ids = $product->getCategoryIds();
foreach ($ids as $id) $catIds[$id] = 1;
}
}
if (!isset($catIds)) return false;
$categories = array();
// Filters rest of categories
if ($filterCategoryId) {
$filterCategory = Mage::getModel('catalog/category')->load($filterCategoryId);
$filterChildren = $filterCategory->getAllChildren(true);
foreach ($catIds as $id => $x) {
$category = Mage::getModel('catalog/category')->load($id);
if (in_array($root_category_id, $category->getParentIds()) && in_array($id, $filterChildren) && $id != $filterCategoryId) {
$categories[$id] = $category->getName();
} else {
unset($catIds[$id]);
}
}
} else {
// Filters the first level categoires
foreach ($catIds as $id => $x) {
$category = Mage::getModel('catalog/category')->load($id);
if (in_array($root_category_id, $category->getParentIds()) && $category->getLevel() == 2) {
$categories[$id] = $category->getName();
} else {
unset($catIds[$id]);
}
}
}
if(isset($categories) && sizeof($categories) > 0):
$url = Mage::app()->getRequest()->getRequesturi();
$url = Mage::helper("core/url")->removeRequestParam($url, 'cat');
?>
<div class="page-subtitle" style="margin:6px auto 14px 6px;"><h2>Shop By Category</h2></div>
<ul class="brandsnav">
<?php foreach($categories as $id=>$name): ?>
<li class="cf">
<span class="subnav_trigger"></span>
<a href="<?php
echo Mage::helper("core/url")->addRequestParam($url, array("cat"=>$id)); ?>"><?php echo $name;?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>