Magento 2 get min price or max price from product collection

Today we learn about how to get min price and max price from product collection. First you have to get collection of product. Then their two predefined function for get min price getMinPrice and get max price getMaxPrice Now you can get the collection following tutorial Magento 2 get product collection.

Now we implement the code for getting min price and max price.

protected $_productCollectionFactory;

public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory
    ) {
        $this->_productCollectionFactory = $productFactory;
    }
    public function getProductCollection()
    {
      $productCollection = $this->_productCollectionFactory->create();
      $maxPrice = $productCollection>getMaxPrice();
     $minPrice = $productCollection>getMinPrice();
    }

}

You can see the example of Min Price and Max Price in Layer collection.Follow Magento\Catalog\Model\Layer\Filter\AbstractFilter.php

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

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

One thought on “Magento 2 get min price or max price from product collection

Leave a Reply