How to Get Product Collection in Magento 2
In this article we'll discuss how to get product collection in magento 2 in 2 different ways. First we'll get product collection using a factory method in a block file and in a second method we'll get product collection using ObjectManager but I'll recommended you to use Dependency Injection rather than directly using ObjectManager.
Let's get started!!
Use following code to get product collection in block file.
saveCopyzoom_out_mapnamespace Devhooks\Module\Block;
class Module extends \Magento\Framework\View\Element\Template
{
protected $productFactory;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory,
array $data = []
)
{
$this->productFactory = $productFactory;
parent::__construct($context, $data);
}
public function getProductCollection()
{
$collection = $this->productFactory->create();
return $collection;
}
}
In above code we use factory option and factory option uses lazy loading, so your product model didn't provide all attributes. If you need the full product data, then you need to load particular product collection see below code for more detail.
saveCopyzoom_out_map$collection = $this->productFactory->create(); return $collection->load($product->getIdBySku($sku));
Now, we'll print the product collection in .phtml file. Check following code to get product collection in phtml file. In this code we just call getProductCollection method from our block.
saveCopyzoom_out_map$productCollection = $block->getProductCollection(); foreach ($productCollection as $product) { echo "<pre>"; print_r($product->getData()); echo "</pre>"; }
You can also get the product collection using objectManager but it's not recommended way.
saveCopyzoom_out_map$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollection->create() ->addAttributeToSelect('*') ->load(); foreach ($collection as $product){ echo "<pre>"; print_r($product->getData()); echo "</pre>"; }
Finally, we are done, let's flush or refresh cache and test the result.
That’s it.
I hope this article helped you to find what you were looking for. Bookmark it for your future reference. Do comment below if you have any other questions on that.
P.S. Do share this article with your team.
AI-Powered Recommended Articles
Get Product Collection with a Specific Attributes in Magento 2
Learn how to retrieve product collections with specific attributes in Magento 2 using custom queries.
Magento 2 Get Product Collection By Cetegory ID
Learn how to retrieve a product collection by category ID in Magento 2 for customized product listings and filters.
How to Get Current Product in Magento 2
Learn how to retrieve the current product in Magento 2 to dynamically display product-specific information on your website.
Magento 2 Get Product Collection with Filters
Learn how to get a product collection in Magento 2 with custom filters for enhanced product retrieval and display.
How to Get Salable Quantity in Magento 2
Learn how to retrieve the salable quantity of a product in Magento 2 to ensure accurate inventory tracking and display on your site.
How to Get Current Category in Magento 2
Learn how to retrieve the current category in Magento 2 using code to display relevant category data in your store.