Magento 2 – How to Change Category Filter Position in Layered Navigation

filterlayered-navigationmagento2position;

I have to move category filter after my custom attribute filter but I am not able to how to move it please help me how to change position. I want to move category filter in the last position.

Best Answer

The filters in the layered navigation are being build in this file:

vendor/magento/module-catalog/Model/Layer/FilterList.php

    /**
 * Retrieve list of filters
 *
 * @param \Magento\Catalog\Model\Layer $layer
 * @return array|Filter\AbstractFilter[]
 */
public function getFilters(\Magento\Catalog\Model\Layer $layer)
{
    if (!count($this->filters)) {
        $this->filters = [
            $this->objectManager->create($this->filterTypes[self::CATEGORY_FILTER], ['layer' => $layer]),
        ];
        foreach ($this->filterableAttributes->getList() as $attribute) {
            $this->filters[] = $this->createAttributeFilter($attribute, $layer);
        }
    }
    return $this->filters;
}

The result of $this->filters will look like

$this->filters[0] = Magento\CatalogSearch\Model\Layer\Filter\Category
$this->filters[1] = Magento\CatalogSearch\Model\Layer\Filter\Attribute
$this->filters[2] = Magento\CatalogSearch\Model\Layer\Filter\Attribute
$this->filters[3] = Magento\CatalogSearch\Model\Layer\Filter\Attribute
$this->filters[4] = Magento\CatalogSearch\Model\Layer\Filter\Attribute

With a custom plugin afterGetFilters you should be able to check if the first item in the array is instanceof Magento\CatalogSearch\Model\Layer\Filter\Category. If so, place it at the end of the array before returning the result.

Related Topic