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.
2 Answers
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.
-
Hello I want to move category filter on 2 position after color, how can I achieve this?– ArjunCommented Mar 17, 2021 at 10:21
work for me
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);
}
}
$data = array_shift($this->filters);
$this->filters[]= $data;
return $this->filters;
}
-
Hello I want to move category filter on 2 position after color, how can I achieve this?– ArjunCommented Mar 17, 2021 at 10:21