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.
count
FROMcatalog_product_entity
ASe
.....