4

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;
        $this->layerResolver->setCurrentCategory($categoryId);
        $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
               $j++;
           }
           if(!empty($filterValues))
           {
               $filterArray['availablefilter'][$availablefilter] =  $filterValues;
           }
           $i++;
       }
        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 – Vishwas Bhatnagar Feb 19 '18 at 8:19
  • What is the class for $this->layerResolver ? – Vignesh Bala Mar 3 '18 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 '18 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..... – Vignesh Bala Mar 6 '18 at 10:24
  • Try this magecom.wordpress.com/page/2 – SIBHI S Mar 6 '18 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.