We've created an extension that does exactly that: https://github.com/Sebwite/magento2-category-sidebar.
$category
has a function getChildrenNodes()
that returns all children. The function only works when flat categories are enabled. We use this function to retrieve subcategories for a category:
public function getSubcategories($category)
{
if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource())
return (array)$category->getChildrenNodes();
return $category->getChildren();
}
I was having with display of category and did some work around and edited code at 2 different files.
You have hard coded root category id as 1 but my root cat id was 2, So in block/sidebar.php, I changed it to store root category.
and 2nd I have changed.
copy following code from page-layout/2columns-left.xml and paste in layout/default.xml
Remove page-layout directory.
This is what I understood from your question. You want to remove category filter for your two top categories (for them, isAnchor
is set to Yes
) and keep all other filters as it is.
To achieve this, most easiest solution is a category specific layout update via any of your layout update xml file. For example, your category id be 12
. Then add below code in your local.xml
file.
File : app\design\frontend\[package]\[theme]\layout\local.xml
<layout>
<CATEGORY_12>
<reference name="catalog.leftnav">
<action method="unsetChild"><alias>category_filter</alias></action>
</reference>
</CATEGORY_12>
</layout>
Here CATEGORY_12
is a layout handle update which is specifically created for a category with an id 12. Inside that, we are basically removing category filter from layered navigation.
Hope you get the idea.
EDIT
So you want to remove category filter and keep all other filters intact. To do that, I believe you need to rewrite the class Mage_Catalog_Block_Layer_View
which you can find at app/code/core/Mage/Catalog/Block/Layer/View.php
protected function _prepareLayout()
{
$stateBlock = $this->getLayout()->createBlock($this->_stateBlockName)
->setLayer($this->getLayer());
$categoryBlock = $this->getLayout()->createBlock($this->_categoryBlockName)
->setLayer($this->getLayer())
->init();
$this->setChild('layer_state', $stateBlock);
$this->setChild('category_filter', $categoryBlock);
//CATEGORY IDS IN WHICH YOU WANT TO HIDE OTHER FILTERS
$excludeCategories = array(12, 13); //example
$currentCategory = Mage::registry('current_category');
//avoid rendering attribute filters for exclude categories
if (!in_array($currentCategory->getId(), $excludeCategories)) {
$filterableAttributes = $this->_getFilterableAttributes();
foreach ($filterableAttributes as $attribute) {
if ($attribute->getAttributeCode() == 'price') {
$filterBlockName = $this->_priceFilterBlockName;
} elseif ($attribute->getBackendType() == 'decimal') {
$filterBlockName = $this->_decimalFilterBlockName;
} else {
$filterBlockName = $this->_attributeFilterBlockName;
}
$this->setChild($attribute->getAttributeCode() . '_filter',
$this->getLayout()->createBlock($filterBlockName)
->setLayer($this->getLayer())
->setAttributeModel($attribute)
->init());
}
}
$this->getLayer()->apply();
return parent::_prepareLayout();
}
Use your category ids in $excludeCategories
and you are good to go.
Best Answer
If you open sidebar.php, you will see, in the function
getChildCategoryView
to fetch the subcategories funtiongetSubcategories
is called. The functiongetSubcategories
will return children Nodes only if you enable "Use Flat Catalog Category" in the Admin settings.1.You have to enable "Use Flat Catalog Category"
2.Run re-indexing command. php bin/magento indexer:reindex
3.Flush Cache php bin/magento cache:flush