Magento 2 remove category filter from list page filters?
You have to override core file into your theme, Category layered navigation comes form core file,
vendor/magento/module-layered-navigation/view/frontend/templates/layer/view.phtml
You have to get files from core and keep in your theme,
app/design/frontend/{Vendor}}/{themename}}/Magento_LayeredNavigation/templates/layer/view.phtml
Now you can set condition for this,
<?php if($filter->getName() != __('Category')) { } ?>
in code.
<?php
/**
* Category layered navigation
*
* @var $block \Magento\LayeredNavigation\Block\Navigation
*/
?>
<?php if ($block->canShowBlock()): ?>
<div class="block filter">
<div class="block-title filter-title">
<strong><?php /* @escapeNotVerified */ echo __('Shop By') ?></strong>
</div>
<div class="block-content filter-content">
<?php echo $block->getChildHtml('state') ?>
<?php if ($block->getLayer()->getState()->getFilters()): ?>
<div class="block-actions filter-actions">
<a href="<?php /* @escapeNotVerified */ echo $block->getClearUrl() ?>" class="action clear filter-clear"><span><?php /* @escapeNotVerified */ echo __('Clear All') ?></span></a>
</div>
<?php endif; ?>
<?php $wrapOptions = false; ?>
<?php foreach ($block->getFilters() as $filter): ?>
<?php if (!$wrapOptions): ?>
<strong role="heading" aria-level="2" class="block-subtitle filter-subtitle"><?php /* @escapeNotVerified */ echo __('Shopping Options') ?></strong>
<dl class="filter-options" id="narrow-by-list">
<?php $wrapOptions = true; endif; ?>
<?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 } ?>
<?php endforeach; ?>
<?php if ($wrapOptions): ?>
</dl>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
Better to use as:
<?php if($filter->getRequestVar() != 'cat') {
// ...
}
?>
Since this is the top result in Google for my search "magento 2 remove category filter from layered navigation", and all the answers suggest entirely replacing a template file (which is subject to Theme-level discrepancies) I thought I'd provide an answer that does not involve a .phtml
template replacement.
The Category filter can be removed from Layered Navigation "Shopping Options" list by using a Plugin.
In your module's etc/di.xml
file, define the plugin and class DI properties:
<type name="Magento\Catalog\Model\Layer\FilterList">
<plugin name="remove_layered_nav_category_filter" type="Vendor\Module\Plugin\Catalog\Model\Layer\FilterList" sortOrder="1" disabled="false" />
</type>
<type name="Vendor\Module\Plugin\Catalog\Model\Layer\FilterList">
<arguments>
<argument name="categoryFilters" xsi:type="array">
<item name="catalogsearch_category" xsi:type="string">Magento\CatalogSearch\Model\Layer\Filter\Category</item>
<item name="category" xsi:type="string">Magento\Catalog\Model\Layer\Filter\Category</item>
</argument>
</arguments>
</type>
And declare the Plugin class:
namespace Vendor\Module\Plugin\Catalog\Model\Layer;
class FilterList
{
const CATALOG_CATEGORY_FILTER = 'category';
const CATALOG_SEARCH_CATEGORY_FILTER = 'catalogsearch_category';
/**
* @var array
*/
protected $categoryFilterTypes = [
self::CATALOG_SEARCH_CATEGORY_FILTER => \Magento\CatalogSearch\Model\Layer\Filter\Category::class,
self::CATALOG_CATEGORY_FILTER => \Magento\Catalog\Model\Layer\Filter\Category::class
];
/**
* FilterList constructor.
*
* @param array $categoryFilters
*/
public function __construct(array $categoryFilters)
{
$this->categoryFilterTypes = array_merge($this->categoryFilterTypes, $categoryFilters);
}
/**
* Remove Category Filter from FilterList
*
* @param \Magento\Catalog\Model\Layer\FilterList $subject
* @param $result
* @param \Magento\Catalog\Model\Layer $layer
* @return array
*/
public function afterGetFilters(
\Magento\Catalog\Model\Layer\FilterList $subject,
$result,
\Magento\Catalog\Model\Layer $layer
) {
foreach ($result as $idx => $filter) {
if ($this->isCategoryFilter($filter)) {
unset($result[$idx]);
break;
}
}
return array_values($result);
}
/**
* Determine if given Filter is one of the declared Category Filter types
*
* @param $filter
* @return bool
*/
private function isCategoryFilter($filter)
{
return \in_array(get_class($filter), $this->categoryFilterTypes);
}
}
This leaves other Filters (e.g., Attribute, Price, Decimal) in place while removing any of the possible Category filters. Should additional Category filters be introduced, a simple change to DI configuration is all that is necessary to extend the functionality.