3

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.

  • 1
    In which class you are trying to get it ? Post your full code here – Keyur Shah Oct 6 '17 at 11:21
4

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

  • @Qaisar..i updated my script. how can i get the product collection with filter object. – Ramesh Oct 6 '17 at 11:47
  • @Ramesh i updated the answer. – Qaisar Satti Oct 6 '17 at 11:57
  • Hi, I'm using Magento 2.2.4 version. How can i implement the script above in my FilterRenderer.php using D.I. approach? Sorry, I'm a Magento newbie. – Jake Pucan Nov 8 '18 at 4:42
  • @JakePucan follow blog.qaisarsatti.com/magento_2/magento-2-get-product-collection after that use this will give you desire result. $block->getProductCollection()->getMaxPrice(); $block->getProductCollection()->getMinPrice(); – Qaisar Satti Nov 8 '18 at 5:34
  • @QaisarSatti I've red that already but how can I set that in filter.php github.com/emizentech/magento2-price-slider/blob/master/view/… Do i need to remove $range = $this->getPriceRange($filter); ? Since the min & max price are from that function. – Jake Pucan Nov 8 '18 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.