1<?php
2namespace Foungento\Theme\Block;
3class Theme extends \Magento\Framework\View\Element\Template
4{
5 protected $_productCollectionFactory;
6
7 public function __construct(
8 \Magento\Backend\Block\Template\Context $context,
9 \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
10 array $data = []
11 )
12 {
13 $this->_productCollectionFactory = $productCollectionFactory;
14 parent::__construct($context, $data);
15 }
16
17 public function getProductCollection()
18 {
19 $collection = $this->_productCollectionFactory->create();
20 $collection->addAttributeToSelect('*');
21 $collection->setPageSize(10); // fetching only 10 products
22 return $collection;
23 }
24}
25?>
26
27/*Display product collection in phtml file
28Print out the product collection in phtml file with the below code:*/
29
30list.phtml
31$productCollection = $block->getProductCollection();
32foreach ($productCollection as $product) {
33 print_r($product->getData());
34 echo "<br>";
35}
1$om = \Magento\Framework\App\ObjectManager::getInstance();
2$productCollection = $om->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
3$collection = $productCollection->addAttributeToSelect('*')->load();