Magento - get product collection of all products

会有一股神秘感。 提交于 2019-12-21 05:35:14

问题


I need a custom product collection of all products. Currently there are no categories which contain all products of the store (as there are 8000 products we can not add them in to one extra category).

What I need is on a particular CMS page the product collection of all products is displayed. So far I have a CMS page with the block:

{{block type="catalog/product_list" template="catalog/product/list.phtml"}}

I have created an module to override 'Mage_Catalog_Block_Product_List'

I believe the function I need to edit would be 'protected function _getProductCollection()'

As we can see in the block call theres no category specified. What I need is in the overidden _getProductCollection function is all products in the store returned.

Is there any way this can be achieved?


回答1:


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>';
}
?>



回答2:


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"}}


来源:https://stackoverflow.com/questions/15087798/magento-get-product-collection-of-all-products

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