skip to Main Content

here is my block file:
path:-VendorModuleBlock

<?php 

namespace VendorModuleBlock;

class Product extends MagentoFrameworkViewElementTemplate
{
    protected $_productCollectionFactory ;

    public function __construct(
        MagentoBackendBlockTemplateContext $context,        
        MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,        
        array $data = []
    )
    {    
        $this->_productCollectionFactory  = $productCollectionFactory;    
        parent::__construct($context, $data);
    }

    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        return $collection;
    }
}

here is my template file:
path:-VendorModuleviewfrontendtemplates

<?php 

    $collection = $block->getProductCollection();
    print_r($collection);
?>

can any one help me with this part?
Thanks in Advance!

2

Answers


  1. <?php
    $collection = $block->getProductCollection();
    foreach($collection as $product){
        print_r($product->getData());
        echo "<br>";
    }
    
    Login or Signup to reply.
  2. Try $block->getProductCollection()->getItems() in the template. It returns an array of products.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search