Magento - get product collection of all products

跟風遠走 提交于 2019-12-03 20:14:38

There are several ways you can get the list of products from a store. Try this way :

<?php
$_productCollection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSort('created_at', 'DESC')
                        ->addAttributeToSelect('*')
                        ->load();
foreach ($_productCollection as $_product){
   echo $_product->getId().'</br>';
   echo $_product->getName().'</br>';
   echo $_product->getProductUrl().'</br>';
   echo $_product->getPrice().'</br>';
}
?>

Don't override the List block, this will have an effect on the real product listing pages.

The simple way to to copy the file to the local namespace and rename it:

from:

app/code/core/Mage/Catalog/Block/Product/List.php

to:

app/code/local/Mage/Catalog/Block/Product/Fulllist.php

You can then use your new block without having to make a full module, and it will mean your List block will work the same and not break anything on your store.

You can then safely modify the as required:

/**
 * Retrieve loaded category collection
 *
 * @return Mage_Eav_Model_Entity_Collection_Abstract
 */
protected function _getProductCollection()
{
    $collection = Mage::getModel('catalog/product')->getCollection();

    // this now has all products in a collection, you can add filters as needed.

    //$collection
    //    ->addAttributeToSelect('*')
    //    ->addAttributeToFilter('attribute_name', array('eq' => 'value'))
    //    ->addAttributeToFilter('another_name', array('in' => array(1,3,4)))
    //;

    // Optionally filter as above..

    return $collection;
}

You can then use your new block like so:

{{block type="catalog/product_fulllist" template="catalog/product/list.phtml"}}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!