Magento 2 – How To Get Product Collection?

March 5, 2020
March 5, 2020 yogita khasturi

Magento 2 – How To Get Product Collection?

Today we will learn very interesting things. which is getting product collection with two ways, Let’s see how they work.

With Dependency Injection:

We will get the list of products in our Magento 2 store using Dependency Injection. First, we need to inject the object of \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory class in the constructor of your module’s block class and then access it from template file “.phtml“.
Sample file path you will use is app/code/Webiators/CustomChanges/Block/Product.php

Now, we will create PHTML file and call Block functions. let’s take a look

With Object Manager:

Now, let’s see how can we call Product collection with Object Manager method.

that’s 2 methods to get product collection in Magento 2. I hope that they are simple enough for you to follow. Thanks for reading!