Magento – How to Remove Categories Filter from the Sidebar

filtermagento-2.1magento2sidebar

in Magento 2.1, I would like to have the Category filter removed from the sidebar when in the shop category page, leaving the price filter untouched.

So far I have tried unsetting the category as anchor, which doesn't change anything. I am able to remove the whole sidebar with XML, but that's not ideal because I want to keep using the price filter.

Best Answer

Override view.phtml file in your theme

From

vendor/magento/module-layered-navigation/view/frontend/templates/layer/view.phtml

To

app/design/frontend/{Vendor}/{theme}/Magento_LayeredNavigation/templates/layer/view.phtml

Now set condition for category before $filter->getItemsCount(),

<?php  

if($filter->getName() != __('Category')) {
    ...
    ...
} 

?>

Final code looks like this

...
...
<?php $wrapOptions = false; ?>
<?php foreach ($block->getFilters() as $filter): ?>
  ...
    <?php  if($filter->getName() != __('Category')) : ?>
        <?php if ($filter->getItemsCount()): ?>
            <dt role="heading" aria-level="3" class="filter-options-title"><?php echo $block->escapeHtml(__($filter->getName())) ?></dt>
            <dd class="filter-options-content"><?php /* @escapeNotVerified */ echo $block->getChildBlock('renderer')->render($filter); ?></dd>
        <?php endif; ?>
    <?php endif; ?>
<?php endforeach; ?>
...
...