Magento - 获取所有产品的产品集合
我需要一个所有产品的自定义产品集合。目前没有包含商店所有产品的类别(因为有8000种产品我们不能将它们添加到一个额外类别中)。Magento - 获取所有产品的产品集合
我需要的是在特定的CMS页面上显示所有产品的产品集合。 到目前为止,我有块一个CMS页面:我已经创建了一个模块来重写“Mage_Catalog_Block_Product_List”
我相信我需要编辑功能将是
{{block type="catalog/product_list" template="catalog/product/list.phtml"}}
'保护功能_getProductCollection()
正如我们所看到的在块调用theres没有指定类别。我需要的是在overidden中_getProductCollection函数是商店中返回的所有产品。
这有什么办法可以实现吗?
不要重写List块,这会影响真正的产品列表页面。
简单的方法来将文件复制到本地命名空间并将其重命名:
来自:
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中停止)。 – 2133215543 2013-02-26 14:46:47
你能修改你的索引吗?php文件来启用DeveloperMode(true)和display_errors = 1,这会给你一个更好的错误信息 – Andrew 2013-02-26 14:49:02
我试过代码if(isset($ _ SERVER ['MAGE_IS_DEVELOPER_MODE'])){Mage :: setIsDeveloperMode(true); ini_set('display_errors',1);在我的索引文件中,但仍然没有收到错误消息。 – 2133215543 2013-02-26 15:09:09
有几种方法你可以从产品列表一家商店。 试试这个方法:
<?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>';
}
?>
你确实需要的所有产品的产品集合,或者你只需要显示所有产品在一个页面上,你要尝试的路线是产品系列? – 2013-02-26 11:28:38
我需要先获取网站的所有产品,然后进一步过滤,以便只显示具有特定属性值的产品。由于产品涉及多个类别,因此我认为我会首先获取产品集合中的所有产品,然后进一步对其进行过滤,以便与list.phtml视图显示类似。 – 2133215543 2013-02-26 11:32:03
糟糕的主意,这将使您的网站非常缓慢。你应该使用产品集合,但是* first *'addAttributeToFilter()'和* then *'load()'。 – 2013-02-26 16:32:37