4

I would like to implement below REST API in magento 2

"How to retrieve which layered navigation filters are applied as like in the product list page?"

Can you please suggest me how we can achieve (or) possibilities?

enter image description here

In magento 1 at website level we can get applied filters at list page (list.phtml) by following snippet

$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();

(and)

$appliedFilters = Mage::getSingleton(‘Mage_Catalog_Block_Layer_State’)->getActiveFilters();

But in magento 2 at api level how we can implement ?

  • Hello @NagarajuKasa Have you got any solution for this ? – Aditya Shah Oct 9 '18 at 11:53
  • 1
    No @adityA shah – Nagaraju Kasa Oct 10 '18 at 2:56
1
$this->layerResolver->setCurrentCategory($id);
            $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 = (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);

I Have implemented same as in list page , but not i need to develop for the search result page , how could i?

Your Answer

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

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