php Magento - 获取所有产品的产品集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15087798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento - get product collection of all products
提问by 2133215543
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).
我需要所有产品的自定义产品集合。目前没有包含商店所有产品的类别(因为有 8000 种产品,我们无法将它们添加到一个额外的类别中)。
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:
我需要的是在特定的 CMS 页面上显示所有产品的产品集合。到目前为止,我有一个带有块的 CMS 页面:
{{block type="catalog/product_list" template="catalog/product/list.phtml"}}
I have created an module to override 'Mage_Catalog_Block_Product_List'
我创建了一个模块来覆盖“Mage_Catalog_Block_Product_List”
I believe the function I need to edit would be 'protected function _getProductCollection()'
我相信我需要编辑的函数是“受保护的函数 _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.
正如我们在块调用中看到的那样,没有指定类别。我需要的是在覆盖的 _getProductCollection 函数中返回商店中的所有产品。
Is there any way this can be achieved?
有什么办法可以实现吗?
回答by stalinrajindian
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>';
}
?>
回答by Andrew
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.
然后,您可以使用新块而无需制作完整的模块,这意味着您的 List 块将保持相同的工作方式并且不会破坏您商店中的任何内容。
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"}}

