Magento - 获得所有产品的产品系列

时间:2013-02-26 11:22:12

标签: php magento collections categories product

我需要所有产品的定制产品系列。目前没有包含商店所有产品的类别(因为有8000种产品我们无法将它们添加到一个额外的类别中)。

我需要的是在特定的CMS页面上显示所有产品的产品系列。 到目前为止,我有一个带有块的CMS页面:

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

我创建了一个模块来覆盖'Mage_Catalog_Block_Product_List'

我相信我需要编辑的功能是'protected function _getProductCollection()'

正如我们在块调用中看到的那样,没有指定类别。我需要的是在覆盖的_getProductCollection函数中,商店中的所有产品都返回了。

有什么办法可以实现吗?

2 个答案:

答案 0 :(得分:12)

您可以通过多种方式从商店获取商品列表。 试试这个:

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

答案 1 :(得分:3)

不要覆盖列表块,这会对真实的产品列表页面产生影响。

将文件复制到本地命名空间并重命名的简单方法:

从:

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

为:

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

然后您可以使用新的块而无需制作完整的模块,这意味着您的列表块将起作用并且不会破坏商店中的任何内容。

然后您可以根据需要安全地修改:

/**
 * 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;
}

然后您可以像这样使用新块:

{{block type="catalog/product_fulllist" template="catalog/product/list.phtml"}}
相关问题