How to Get Product Collection in Magento 2?

  • Nidhi Arora
  • 5 years
  • 62 views
image_pdfimage_print

If you are a developer who uses Magento 1.x, you will find yourself deeply aware about the usage of “Mage::getModel(‘catalog/product’)->getCollection();” to get product collection. However, if you are using Magento 2 and you want your product collection, it is quite different as compared to Magento 1.x.

So with no further delay, let’s have a look at the code mentioned below:

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

/* @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection /

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');

/* Apply filters here /

$productCollection->load();

var_dump($productCollection->getData());

?>

After reviewing afore shared code, we believe that you can easily understand & perform the required steps to get the product collection in Magento 2. If you still have any query regarding applying this coding in your Magento 2, or any other inquiry related to Magento 2, you can leave a comment below or contact us.

Join us for a FREE webinar