Magento 2 api - How to get layer navigation filters available in rest api?
There are 2 methods to achieve this
First one
Filter.php (api model)
public function __construct(\Magento\Framework\Webapi\Rest\Request
$request){
$this->_request = $request;
}
/**
* Retrieve filterlist
*
* @api
* @return array
*/
public function retrieve(){
$category = $this->_request->getParam('categoryId');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$filterableAttributes = $objectManager->getInstance()->get(\Magento\Catalog\Model\Layer\Category\FilterableAttributeList::class);
$appState = $objectManager->getInstance()->get(\Magento\Framework\App\State::class);
$layerResolver = $objectManager->getInstance()->get(\Magento\Catalog\Model\Layer\Resolver::class);
$filterList = $objectManager->getInstance()->create(
\Magento\Catalog\Model\Layer\FilterList::class,
[
'filterableAttributes' => $filterableAttributes
]
);
$layer = $layerResolver->get();
$layer->setCurrentCategory($category);
$filters = $filterList->getFilters($layer);
$maxPrice = $layer->getProductCollection()->getMaxPrice();
$minPrice = $layer->getProductCollection()->getMinPrice();
$i = 0;
foreach($filters 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]['value'] = $item->getValue();
$filterValues[$j]['count'] = $item->getCount(); //Gives no. of products in each filter options
$j++;
}
if(!empty($filterValues) && count($filterValues)>1)
{
$filterArray['availablefilter'][$availablefilter] = $filterValues;
}
$i++;
}
echo json_encode($filterArray);
exit;
}
Sample out put of this code will be:
{
"availablefilter": {
"Climate": [
{
"display": "All-Weather",
"value": "202",
"count": "1"
},
{
"display": "Cool",
"value": "204",
"count": "1"
}
],
"Manufacturer": [
{
"display": "Azzedine Alaïa",
"value": "216",
"count": "1"
},
{
"display": "Balenciaga",
"value": "217",
"count": "1"
}
],
"Material": [
{
"display": "Cotton",
"value": "33",
"count": "1"
},
{
"display": "Nylon",
"value": "37",
"count": "1"
}
],
Second method
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $productAttributeCollectionFactory
) {
$this->productAttributeCollectionFactory = $productAttributeCollectionFactory;
}
public function getFilterableAttributes()
{
/** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection $productAttributes */
$productAttributes = $this->productAttributeCollectionFactory->create();
$productAttributes->addFieldToFilter(
['is_filterable', 'is_filterable_in_search'],
[[1, 2], 1]
);
return $productAttributes;
}
Get request value in json format like below
{
"category_id":"36" //pass category which you want
}
below is the Filterattributes.php (api model)
class FilterAttributes extends \Magento\Framework\Model\AbstractModel implements FilterAttributesInterface
{
public function __construct(
){
}
/**
* Returns greeting message to user
*
* @api
* @param string $name Users name.
* @return string Greeting message with users name.
*/
public function filterAttributes() {
$json = file_get_contents('php://input');
$post = json_decode($json);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$filterableAttributes = $objectManager->getInstance()->get(\Magento\Catalog\Model\Layer\Category\FilterableAttributeList::class);
$appState = $objectManager->getInstance()->get(\Magento\Framework\App\State::class);
$layerResolver = $objectManager->getInstance()->get(\Magento\Catalog\Model\Layer\Resolver::class);
$filterList = $objectManager->getInstance()->create(
\Magento\Catalog\Model\Layer\FilterList::class,
[
'filterableAttributes' => $filterableAttributes
]
);
$layer = $layerResolver->get();
$layer->setCurrentCategory($post->category_id);
$filters = $filterList->getFilters($layer);
$maxPrice = $layer->getProductCollection()->getMaxPrice();
$minPrice = $layer->getProductCollection()->getMinPrice();
$i = 0;
$filterAttrs = [];
foreach($filters as $filter)
{
$values = [];
$attr_code = (string)$filter->getRequestVar();
$attr_label = (string)$filter->getName();
$items = $filter->getItems(); //Gives all available filter options in that particular filter
foreach($items as $item)
{
$values[] = array("display"=>strip_tags($item->getLabel()),
"value"=>$item->getValue(),
"count"=>$item->getCount());
}
if(!empty($values) && count($values)>1)
{
$filterAttrs[]=array("attr_code"=>$attr_code,
"attr_label"=>$attr_label,
"values"=>$values);
}
}
if(count($filters)>0){
$data['status'] = "true";
$data['filters'] = $filterAttrs;
}else{
$data['status'] = "false";
$data['msg'] = "No Filters Found";
}
echo json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT );// str_replace('\/','/',json_encode($data));
exit();
}
}