6

I have defined a featured product template in a cms static page as below:

{{block class="Magento\Catalog\Block\Product\ListProduct" template="Magento_Catalog::product/featured.phtml"}}

Now I want to load original product collection(without any filters applied, which I am gonna apply later) inside my template "featured.phtml", but I can't find a way to do it directly without creating a module for such a tiny task.

Somebody let me know, how to do something like below in Magento2, in this case: Mage::getModel('catalog/product')->getCollection()

2
  • Have you tried \Magento\Catalog\Block\Product\ListProduct::getLoadedProductCollection? It is accessible in your template just as $this->getLoadedProductCollection(). Dec 1 '15 at 11:37
  • Yes tried that already, but it is already a filtered collection with very less products, I need raw colleciton without any filters like $this->getCollection()
    – Vicky Dev
    Dec 1 '15 at 11:39
12

It is better to create custom block for your needs. It is not clear why you can create custom template, but not block. Also have you considered using \Magento\Catalog\Api\ProductRepositoryInterface::getList which is part of Magento public API? Collection should not be manipulated directly.

Implementation below is a hack (object manager should never be used directly), but the only solution without creation of a new block:

$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();
5
  • Thanks for reply, can you tell me how to filter this collection by category_id, I tried filtering using function addFieldToFilter but it gives error, I guess addField.... function only works for table's own fields not with joining tables.
    – Vicky Dev
    Dec 1 '15 at 12:27
  • 1
    $productCollection->addCategoriesFilter(['in' => [1, 2, 3]]) should work. Dec 1 '15 at 13:07
  • Thanks, your solution is working perfectly for now. Accepting it.
    – Vicky Dev
    Dec 1 '15 at 13:13
  • @AlexPaliarush How do i use filter by attribute and select attribute like name, id, v.v.. via ProductRepository example:$this->_productRepository->getList($this->_searchCriteria)->addAttributeToFilter('is_featured', 1) results undefined method trigger
    – mrtuvn
    Jan 12 '16 at 10:36
  • @mrtuvn $this->_searchCriteria should contain necessary filters. addAttributeToFilter is defined for collections and is not available for repositories. Take a look at these answers Jan 12 '16 at 14:32
9

Instead of using the Object manager u can try:

public function __construct(
    \Magento\Backend\Block\Template\Context $context,        
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
    array $data = []
)
{    
    $this->_productCollectionFactory = $productCollectionFactory;    
    parent::__construct($context, $data);
}

 public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->setPageSize(3); // fetching only 3 products
    return $collection;
}
5

In your block file:

use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\View\Element\Template\Context;

protected $_productFactory;

public function __construct(
   Context $context, 
   ProductFactory $productFactory,
   array $data = array()       
) {
   $this->_productFactory   = $productFactory;
   parent::__construct($context, $data);
}

public function getProductCollection() {
   $productCollection = $this->_productFactory->create()->getCollection();
   return $productCollection;
}

To get the collection call getProductCollection function.

3
  • 1
    product Factory is used to create new product, don't use it that way Jul 25 '16 at 10:38
  • you can check the default magento 2 files it uses product factory. Jul 26 '16 at 4:06
  • 5
    you should use CollectionFactory instead Jul 26 '16 at 6:17
0

Try this :

    protected $_productCollectionFactory;

    public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        array $data = []
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;
        parent::__construct($data);
    }



    public function getProductCollection()
    {
        /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
        $collection = $this->_productCollectionFactory->create();
        return $collection;
    }
3
  • Not working in Magento 2 stable version.
    – Vicky Dev
    Dec 1 '15 at 11:29
  • updated ans pls check
    – Abdul
    Dec 1 '15 at 11:35
  • I know that by overriding the Block(probably in our own module) it can be done, but I am asking for doing this without module way as I mentioned in my question.
    – Vicky Dev
    Dec 1 '15 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.