【问题标题】:Magento - get product collection of all productsMagento - 获取所有产品的产品集合
【发布时间】:2013-02-26 11:22:12
【问题描述】:

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

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

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

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

我相信我需要编辑的函数是“受保护的函数_getProductCollection()”

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

有什么方法可以实现吗?

【问题讨论】:

  • 您真的需要所有产品的产品集合,还是只需要在一个页面上显示所有产品并且您尝试的路线是产品集合?
  • 我需要首先获取网站的所有产品,然后对其进行进一步过滤,以便仅显示具有特定属性值的产品。由于产品涉及多个类别,我想我会先获取产品集合中的所有产品,然后对其进行进一步过滤,因此它的显示类似于 list.phtml 视图。
  • 坏主意,这会使您的网站非常缓慢。您应该使用产品集合,但 first addAttributeToFilter()then load()

标签: php magento collections categories product


【解决方案1】:

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

<?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】:

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

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

    来自:

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

    到:

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

    然后您可以使用您的新块而无需制作一个完整的模块,这意味着您的 List 块将可以正常工作并且不会破坏您商店中的任何东西。

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

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

    【讨论】:

    • 感谢您的回复。我已按照您的指示添加了新块。但是,当我运行代码主体时,我什么也没有显示(我的页脚消失,​​提示在 php 中停止)。
    • 你能否修改你的 index.php 文件以启用 DeveloperMode(true) 和 display_errors = 1,这会给你一个更好的错误信息
    • 我已经尝试过代码 if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) { Mage::setIsDeveloperMode(true); } 和 ini_set('display_errors', 1);在我的索引文件上,但仍然没有收到错误消息。
    • 注释掉包裹在 Mage::setIsDeveloperMode(true) 周围的 'if';所以它总是被执行
    • 感谢您的回复。我也试过了,但我仍然没有收到任何错误消息。还有其他建议吗?
    猜你喜欢
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多