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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php namespace Webiators\CustomChanges\Block; class Product extends \Magento\Framework\View\Element\Template { protected $_productCollectionFactory; protected $_productVisibility; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Catalog\Model\Product\Visibility $productVisibility, array $data = [] ) { $this->_productCollectionFactory = $productCollectionFactory; $this->_productVisibility = $productVisibility; parent::__construct($context, $data); } public function getProductCollection() { $collection = $this->_productCollectionFactory->create(); $collection->addAttributeToSelect('*'); // filter current website products $collection->addWebsiteFilter(); // filter current store products $collection->addStoreFilter(); // set visibility filter $collection->setVisibility($this->productVisibility->getVisibleInSiteIds()); // fetching 10 products (we can pass number of products) $collection->setPageSize(10); return $collection; } } |
Now, we will create PHTML file and call Block functions. let’s take a look
1 2 3 4 5 6 |
$productCollection = $block->getProductCollection(); foreach ($productCollection as $product) { echo $product->getId() . '<br />'; echo $product->getName() . '<br />'; echo $product->getPrice() . '<br />'; } |
With Object Manager:
Now, let’s see how can we call Product collection with Object Manager method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToSelect('*'); // filter current website products $collection->addWebsiteFilter(); // filter current store products $collection->addStoreFilter(); // set visibility filter $collection->setVisibility($objectManager->get('\Magento\Catalog\Model\Product\Visibility')->getVisibleInSiteIds()); // fetching 10 products (we can pass number of products) $collection->setPageSize(10); foreach ($productCollection as $product) { echo $product->getId() . '<br />'; echo $product->getName() . '<br />'; echo $product->getPrice() . '<br />'; } |
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!