Magento – How to implement multiple category filter in Magento 2 layered navigation

categorylayered-navigationmagento2multiselect-attribute

I am implementing multiple category filters for layered navigation. I am getting the result as per my requirement but the collection size is coming wrong remains same as the parent categories collection size.

Is there any other way to implement this functionality.

I have modified the following code :

public function apply(\Magento\Framework\App\RequestInterface $request)
    {
        $categoryId = $request->getParam($this->_requestVar) ?: $request->getParam('id');
        if (empty($categoryId)) {
            return $this;
        }

        $this->dataProvider->setCategoryId($categoryId);

        $category = $this->dataProvider->getCategory();

        **$this->getLayer()->getProductCollection()->addCategoryFilter($category);**

        if ($request->getParam('id') != $category->getId() && $this->dataProvider->isValid()) {
            $this->getLayer()->getState()->addFilter($this->_createItem($category->getName(), $categoryId));
        }
        return $this;
    }

replaced the addCategoryFilter($category) with addCategoriesFilter('eq' => $categoriesArray);

But as the layered navigation count is coming from somewhere else and not from product collection.

would like to know from where. And how could I apply multiple category filters there?

Best Answer

Please take a look at the research I've done concerning this and the final solution (my answer to the question) here: Magento 2.1.6 Product Grid page count and record count issue when programmatically adding filter using different methods

Related Topic