Magento 2 Layered Navigation – Get Min and Max Price

layered-navigationmagento-2.1magento2

I am trying to get Min & Max price in Layered Navigation and I am using a third party extension for layered navigation..

By using below script, I am trying to get Min & Max price for filter collection.

  public function getPriceRange($filter){
     $Filterprice = array('min' => 0 , 'max'=>100);
    if($filter instanceof Magento\CatalogSearch\Model\Layer\Filter\Price) {
        $priceArr = $filter->getResource()->loadPrices(10000000000);           
        $Filterprice['min'] = reset($priceArr);
        $Filterprice['max'] = end($priceArr);
    }

    return $Filterprice;
}

But it says loadprice not a method. Can any please suggest to get Min & Max price.

Best Answer

you can get min and max price from any collection

For any collection.

$maxPrice = $productCollection->getMaxPrice();
$minPrice = $productCollection->getMinPrice();

Layer collection. Follow Magento\Catalog\Model\Layer\Filter\AbstractFilter.php

$this->getLayer()->getProductCollection()->getMaxPrice();
$this->getLayer()->getProductCollection()->getMinPrice();

Reference