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 ?

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Mar 14, 2018 at 6:59
2
  • Hello @NagarajuKasa Have you got any solution for this ? Commented Oct 9, 2018 at 11:53
  • 1
    No @adityA shah Commented 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?

Manoj Deswal
5,80325 gold badges29 silver badges50 bronze badges
answered Feb 22, 2019 at 9:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.