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//to overwrite limit but you need first to increase your memory limit
2
3 $collection = Mage::getModel('catalog/product')->getCollection()
4->addAttributeToSelect('*') // select all attributes
5->setPageSize(5000) // limit number of results returned
6->setCurPage(1); // set the offset (useful for pagination)
7
8// we iterate through the list of products to get attribute values
9foreach ($collection as $product) {
10 echo $product->getName(); //get name
11 echo (float) $product->getPrice(); //get price as cast to float
12 echo $product->getDescription(); //get description
13 echo $product->getShortDescription(); //get short description
14 echo $product->getTypeId(); //get product type
15 echo $product->getStatus(); //get product status
16
17 // getCategoryIds(); returns an array of category IDs associated with the product
18 foreach ($product->getCategoryIds() as $category_id) {
19 $category = Mage::getModel('catalog/category')->load($category_id);
20 echo $category->getName();
21 echo $category->getParentCategory()->getName(); // get parent of category
22 }
23 //gets the image url of the product
24 echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).
25 'catalog/product'.$product->getImage();
26 echo $product->getSpecialPrice();
27 echo $product->getProductUrl(); //gets the product url
28 echo '<br />';
29}
30