You are viewing an old revision of this post, from February 20, 2020 @ 15:13:20. See below for differences between this version and the current revision.

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 a collection of the product. Then their two predefined functions for getting min price getMinPrice and get max price getMaxPrice

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();

Revisions

  • February 20, 2020 @ 15:13:53 [Current Revision] by Sharing Solution
  • February 20, 2020 @ 15:13:51 by Sharing Solution
  • February 20, 2020 @ 15:13:20 by Sharing Solution

Revision Differences

February 20, 2020 @ 15:13:20Current Revision
Content
Unchanged: <!-- wp:paragraph --> Unchanged: <!-- wp:paragraph -->
Deleted: <p>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 <strong>getMinPrice</strong> and get max price <strong>getMaxPrice< /strong></p>  Added: <p>Today we learn about how to get min price and max price from product collection. First, you have to get a collection of the product. Then their two predefined functions for getting min price <strong>getMinPrice</strong> and get max price <strong>getMaxPrice< /strong></p>
Unchanged: <!-- /wp:paragraph --> Unchanged: <!-- /wp:paragraph -->
Unchanged: <!-- wp:paragraph --> Unchanged: <!-- wp:paragraph -->
Unchanged: <p>Now we implement the code for getting min price and max price.</p> Unchanged: <p>Now we implement the code for getting min price and max price.</p>
Unchanged: <!-- /wp:paragraph --> Unchanged: <!-- /wp:paragraph -->
Unchanged: <!-- wp:paragraph --> Unchanged: <!-- wp:paragraph -->
Unchanged: <p><strong>protected</strong> $_productCollectionFactory; <br><br><strong>public function</strong> __construct(<br>         \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory<br>     ) {<br>         $this->_productCollectionFactory = $productFactory;<br>     } </p> Unchanged: <p><strong>protected</strong> $_productCollectionFactory; <br><br><strong>public function</strong> __construct(<br>         \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory<br>     ) {<br>         $this->_productCollectionFactory = $productFactory;<br>     } </p>
Unchanged: <!-- /wp:paragraph --> Unchanged: <!-- /wp:paragraph -->
Unchanged: <!-- wp:paragraph --> Unchanged: <!-- wp:paragraph -->
Unchanged: <p><br><strong>public function</strong> getProductCollection()<br>     {<br>       $productCollection = $this->_productCollectionFactory- >create();<br>       $maxPrice = $productCollection> getMaxPrice();<br>      $minPrice = $productCollection> getMinPrice();<br>     }<br><br> } </p> Unchanged: <p><br><strong>public function</strong> getProductCollection()<br>     {<br>       $productCollection = $this->_productCollectionFactory- >create();<br>       $maxPrice = $productCollection> getMaxPrice();<br>      $minPrice = $productCollection> getMinPrice();<br>     }<br><br> } </p>
Unchanged: <!-- /wp:paragraph --> Unchanged: <!-- /wp:paragraph -->
Unchanged: <!-- wp:paragraph --> Unchanged: <!-- wp:paragraph -->
Unchanged: <p>You can see the example of Min Price and Max Price in Layer collection.Follow <strong>Magento\Catalog\Model\Layer\Filter\AbstractFilter.php</strong> Unchanged: <p>You can see the example of Min Price and Max Price in Layer collection.Follow <strong>Magento\Catalog\Model\Layer\Filter\AbstractFilter.php</strong>
Unchanged: $this-&gt;getLayer( )-&gt;getProductCollection( )-&gt;getMaxPrice();<br> Unchanged: $this-&gt;getLayer( )-&gt;getProductCollection( )-&gt;getMaxPrice();<br>
Unchanged: $this-&gt;getLayer( )-&gt;getProductCollection( )-&gt;getMinPrice(); Unchanged: $this-&gt;getLayer( )-&gt;getProductCollection( )-&gt;getMinPrice();
Unchanged:  Unchanged:
Unchanged: </p> Unchanged: </p>
Unchanged: <!-- /wp:paragraph --> Unchanged: <!-- /wp:paragraph -->

Note: Spaces may be added to comparison text to allow better line wrapping.

No comments yet.

Leave a Reply