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
  • 1
    In which class you are trying to get it ? Post your full code here
    – Keyur Shah
    Commented Oct 6, 2017 at 11:21

2 Answers 2

5

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

7
  • @Qaisar..i updated my script. how can i get the product collection with filter object.
    – Ramesh
    Commented Oct 6, 2017 at 11:47
  • @Ramesh i updated the answer. Commented Oct 6, 2017 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
    Commented Nov 8, 2018 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(); Commented Nov 8, 2018 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
    Commented Nov 8, 2018 at 5:49
0
public function getPriceRange($filter)
{
    $max = $filter->getLayer()->getProductCollection()->getMaxPrice();
    $min = $filter->getLayer()->getProductCollection()->getMinPrice();
    $Filterprice = ['min' => $min, 'max' => $max];
    return $Filterprice;
}

Your Answer

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

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