How to get product collection in custom template block in Magento 2
To get product collection in custom template block in Magento 2, use the following code. It is a trick, since you should never use object manager directly, but at the same time the only way to avoid creating a new block:
1 |
$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(); |
Source (