Home » Magento, Magento 228 February 2016

Magento 2: Get Product Collection

This article shows how we can get product collection in Magento 2.

Using Dependency Injection (DI)

Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory class in the constructor of my module’s block class.

app/code/Chapagain/HelloWorld/Block/HelloWorld.php

As you can see in above code, I have addded limit to the product collection. I have selected only 3 products.

Now, we print the product collection in our template (.phtml) file.

Using Object Manager

Hope this helps. Thanks.

Magento 2

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed