5

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 ?

2
  • Hello @NagarajuKasa Have you got any solution for this ? Oct 9, 2018 at 11:53
  • 1
    No @adityA shah Oct 10, 2018 at 2:56

1 Answer 1

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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