3

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 2

2

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.

1
  • Hello I want to move category filter on 2 position after color, how can I achieve this?
    – Arjun
    Commented Mar 17, 2021 at 10:21
0

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;
    }
1
  • Hello I want to move category filter on 2 position after color, how can I achieve this?
    – Arjun
    Commented Mar 17, 2021 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.