1

I have successfully created Layer navigation REST API using given solution on this question Magento 2 api - How to get layer navigation filters available in rest api? However I want if someone clicked on a particular brand then other brand should be remove from layer navigation Currently it's return result based on category id, I would like to add more filter like Brand, color etc

 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;

    }

0

Your Answer

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

Browse other questions tagged or ask your own question.