{"id":7281,"date":"2019-09-23T10:47:35","date_gmt":"2019-09-23T05:17:35","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7281"},"modified":"2019-09-27T13:41:47","modified_gmt":"2019-09-27T08:11:47","slug":"magento-2-get-product-collection","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-product-collection\/","title":{"rendered":"Magento 2 &#8211; How to Get Product Collection"},"content":{"rendered":"<p>In this post I am going to explain how to get list of all products or you can say product collection in magento 2.<\/p>\n<p><strong>1. Using Dependency Injection<\/strong><\/p>\n<p>Add below code snippet in Block class.<\/p>\n<pre class=\"lang:default decode:true \">protected $_productCollectionFactory;\r\n        \r\npublic function __construct(\r\n\t\\Magento\\Backend\\Block\\Template\\Context $context,        \r\n\t\\Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory $productCollectionFactory,\r\n\tarray $data = []\r\n)\r\n{\r\n\t$this-&gt;_productCollectionFactory = $productCollectionFactory;\r\n\tparent::__construct($context, $data);\r\n}\r\n\r\npublic function getProductCollection() \r\n{\r\n\t$productCollection = $this-&gt;_productCollectionFactory-&gt;create()\r\n\t\t\t-&gt;addAttributeToSelect('*')\r\n\t\t\t-&gt;addAttributeToFilter('visibility', \\Magento\\Catalog\\Model\\Product\\Visibility::VISIBILITY_BOTH)\r\n\t\t\t-&gt;addAttributeToFilter('status', \\Magento\\Catalog\\Model\\Product\\Attribute\\Source\\Status::STATUS_ENABLED)\r\n\t\t\t-&gt;setOrder('entity_id', 'DESC');\r\n\treturn $productCollection;\r\n}<\/pre>\n<p>Add below code snippet in template file.<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Get product collection\r\n$productCollection = $block-&gt;getProductCollection();\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($productCollection-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\nif ($productCollection &amp;&amp; count($productCollection) &gt; 0) {\r\n    foreach ($productCollection AS $product) {\r\n        echo $product-&gt;getId() . \"&lt;br\/&gt;\";\r\n        echo $product-&gt;getName() . \"&lt;br\/&gt;\";\r\n        echo $product-&gt;getProductUrl() . \"&lt;br\/&gt;\";\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>2. Using Object Manager<\/strong><\/p>\n<pre class=\"lang:default decode:true \">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\r\n\r\n$productCollectionFactory = $objectManager-&gt;create('Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory')-&gt;create();\r\n\r\n\/\/ Get product collection\r\n$productCollection = $productCollectionFactory-&gt;addAttributeToSelect('*')\r\n        -&gt;addAttributeToFilter('visibility', \\Magento\\Catalog\\Model\\Product\\Visibility::VISIBILITY_BOTH)\r\n        -&gt;addAttributeToFilter('status', \\Magento\\Catalog\\Model\\Product\\Attribute\\Source\\Status::STATUS_ENABLED)\r\n        -&gt;setOrder('entity_id', 'DESC');\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($productCollection-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\nif ($productCollection &amp;&amp; count($productCollection) &gt; 0) {\r\n    foreach ($productCollection AS $product) {\r\n        echo $product-&gt;getId() . \"&lt;br\/&gt;\";\r\n        echo $product-&gt;getName() . \"&lt;br\/&gt;\";\r\n        echo $product-&gt;getProductUrl() . \"&lt;br\/&gt;\";\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to get list of all products or you can say product collection in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_productCollectionFactory; public function __construct( \\Magento\\Backend\\Block\\Template\\Context $context, \\Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory $productCollectionFactory, array $data = [] ) { $this-&gt;_productCollectionFactory = $productCollectionFactory; parent::__construct($context, $data);&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-product-collection\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7339,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[10,689,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7281"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7281"}],"version-history":[{"count":2,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7281\/revisions"}],"predecessor-version":[{"id":9823,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7281\/revisions\/9823"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7339"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}