I am developing API to get the navigation filters in catalog and search results page. Using the below code I'm able to get the available filters for a category. I need apply any of the filters to the category and should get the resulted navigation filter. Same this has to be done for search results page also.

$categoryId = 3;
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $fill = $objectManager->create('Magento\Catalog\Model\Layer\Category\FilterableAttributeList');
        $filterList = new \Magento\Catalog\Model\Layer\FilterList($objectManager,$fill);
        $filterAttributes = $filterList->getFilters($this->layerResolver);
        $filterArray = array();
        $i = 0;

       foreach($filterAttributes as $filter)
           //$availablefilter = $filter->getRequestVar(); //Gives the request param name such as 'cat' for Category, 'price' for Price
           $availablefilter = (string)$filter->getName(); //Gives Display Name of the filter such as Category,Price etc.
           $items = $filter->getItems(); //Gives all available filter options in that particular filter
           $filterValues = array();
           $j = 0;
           foreach($items as $item)
               $filterValues[$j]['display'] = strip_tags($item->getLabel());
               $filterValues[$j]['label'] = $item->getValue();
               $filterValues[$j]['count'] = $item->getCount(); //Gives no. of products in each filter options
               $filterArray['availablefilter'][$availablefilter] =  $filterValues;
        return array($filterArray);

Is there any possible way to get navigation filters for a product collection so that it will work for both category and search pages.

  • Any update on this SIBHI ? i was trying to do the same Feb 19, 2018 at 8:19
  • What is the class for $this->layerResolver ? Mar 3, 2018 at 5:11
  • @Vignesh Bala injected this class in constructor \Magento\Catalog\Model\Layer\Resolver $layerResolver and assigned the variable as $this->layerResolver = $layerResolver->get();
    – SIBHI S
    Mar 5, 2018 at 9:00
  • But I got the following error. Next Exception: Report ID: webapi-5a9e6a9cd97ec; Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '_idx.entity_id) AS count FROM catalog_product_entity AS e..... Mar 6, 2018 at 10:24
  • Try this magecom.wordpress.com/page/2
    – SIBHI S
    Mar 6, 2018 at 12:16


Your Answer

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

Browse other questions tagged or ask your own question.