php magento 限制产品收集调用中返回的项目数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2854110/
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 limiting number of returned items in product collection call
提问by thrice801
Im trying to limit the number of returned results manually in a copy of the list.phtml template, but its turning out to be alot harder than I anticipated.
我试图在 list.phtml 模板的副本中手动限制返回结果的数量,但结果比我预期的要困难得多。
Ive tried manually setting the collection size, but ya once again nothing working. Can someone show me how to do this? Would be nmuch appreciated!
我试过手动设置集合大小,但再次没有任何效果。有人可以告诉我如何做到这一点吗?将不胜感激!
回答by clockworkgeek
A quick way is with this method I recently discovered. You can even use it directly in the template.
一种快速的方法是使用我最近发现的这种方法。您甚至可以直接在模板中使用它。
$_productCollection = clone $this->getLoadedProductCollection();
$_productCollection->clear()
->setPageSize(3)
->load();
回答by Jonathan Day
A similar approach to @joseph is to override Mage_Catalog_Block_Product_Listbut insert the following code in your new class:
与@joseph 类似的方法是覆盖Mage_Catalog_Block_Product_List但在新类中插入以下代码:
const PAGE_SIZE = 3;
protected function _getProductCollection(){
$collection = parent::_getProductCollection();
$yourCustomBoolean = someFunctionThatDetectsYourCustomPage();
if($yourCustomBoolean) {
$collection->setPageSize(self::PAGE_SIZE);
}
return $collection;
}
that way you will inherit any future changes in the Mage_Catalog code from the parent block but still set your page limits.
这样,您将从父块继承 Mage_Catalog 代码中的任何未来更改,但仍设置您的页面限制。
Ideally you would use a system.xmlnode to create a field that can be edited by an administrator without hardcoding the page_size. The xml would look something like:
理想情况下,您将使用system.xml节点创建一个可由管理员编辑的字段,而无需对 page_size 进行硬编码。xml 看起来像:
<config>
<sections>
<catalog>
<groups>
<frontend>
<fields>
<custom_page_size translate="label">
<label>Page Size for Custom Page Type</label>
<frontend_type>text</frontend_type>
<sort_order>9999</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</custom_page_size>
</fields>
</frontend>
</groups>
</catalog>
</sections>
</config>
Then retrieve that value in your code with:
然后在您的代码中检索该值:
$page_size = Mage::getStoreConfig('catalog/frontend/custom_page_size');
HTH,
JD
HTH,
JD
回答by Alessandro Ronchi
unfortunately it doesn't work because in the _getProductCollection()method the Collection has been already initialized with a page size.
不幸的是它不起作用,因为在该_getProductCollection()方法中,集合已经用页面大小进行了初始化。
A more flexible solution could be that of observing the catalog_product_collection_load_beforeevent which, as the name suggests, is dispatched before the collection is loaded.
一个更灵活的解决方案可能是观察catalog_product_collection_load_before事件,顾名思义,在加载集合之前调度该事件。
Here follows an example (assuming to write a yourmoduleextension under yourpackage):
下面是一个例子(假设在 下写一个yourmodule扩展yourpackage):
STEP 1: Define your observer in config.xml
第 1 步:在 config.xml 中定义您的观察者
in the globalsection of your config.xmlextension file insert something like:
在global您的config.xml扩展文件的部分插入如下内容:
<events>
<catalog_product_collection_load_before>
<observers>
<yourpackage_yourmodule_catalog_observer>
<type>singleton</type>
<class>yourpackage_yourmodule/catalog_observer</class>
<method>limitPageSize</method>
</yourpackage_yourmodule_catalog_observer>
</observers>
</catalog_product_collection_load_before>
</events>
STEP 2: Define your Observer class under the Model\Catalogfolder:
第 2 步Model\Catalog:在文件夹下定义您的 Observer 类:
<?php
class Yourpackage_Yourmodule_Model_Catalog_Observer
{
public function limitPageSize($observer)
{
#TODO: Insert the logic you need to differentiate when to apply the following
$event = $observer->getEvent();
$collection = $event->getCollection();
$collection->setPageSize(3);
return $this;
}
}
Hope it helps. Sincerely, Alessandro Ronchi
希望能帮助到你。此致,亚历山德罗·隆奇
回答by Sajjucode
I got the Code of User:clockworkgeek , but here is some issue and correct code is as follow, it work and thanks clockworkgeek.
我得到了用户代码:clockworkgeek,但这里有一些问题,正确的代码如下,它可以工作,感谢clockworkgeek。
$_productCollection = $this->getLoadedProductCollection();
$_productCollection->clear();
$_productCollection->setPageSize(3)
$_productCollection->load();
You Also writeenter code hereor solve this issue by modify as
您还可以enter code here通过修改为编写或解决此问题
$this->getLoadedProductCollection()->clear();
$_productCollection = $this->getLoadedProductCollection();
Thanks, If it help you then comment.
谢谢,如果对您有帮助,请评论。
回答by Joseph Mastey
It looks like the collection returned in list.phtml has already had load() called, which means that by the time we get to the template we've lost the opportunity to set the page size. So, this is going to get a bit messy!
看起来 list.phtml 中返回的集合已经调用了 load(),这意味着当我们到达模板时,我们已经失去了设置页面大小的机会。所以,这会变得有点乱!
The block that generates that collection is Mage_Catalog_Block_Product_List, which we can extend with our own class and override at the same time. Create a new block that extends Mage_Catalog_Block_Product_Listand override the method _getProductCollectionas follows:
生成该集合的块是Mage_Catalog_Block_Product_List,我们可以使用我们自己的类扩展并同时覆盖它。创建一个新块来扩展Mage_Catalog_Block_Product_List和覆盖该方法_getProductCollection,如下所示:
/**
* Retrieve loaded category collection
*
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$layer = Mage::getSingleton('catalog/layer');
/* @var $layer Mage_Catalog_Model_Layer */
if ($this->getShowRootCategory()) {
$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
}
// if this is a product view page
if (Mage::registry('product')) {
// get collection of categories this product is associated with
$categories = Mage::registry('product')->getCategoryCollection()
->setPage(1, 1)
->load();
// if the product is associated with any category
if ($categories->count()) {
// show products from this category
$this->setCategoryId(current($categories->getIterator()));
}
}
$origCategory = null;
if ($this->getCategoryId()) {
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}
}
$this->_productCollection = $layer->getProductCollection();
$this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
// OUR CODE MODIFICATION ////////////////////
$yourCustomPage = someFunctionThatDetectsYourCustomPage();
if($yourCustomPage) {
$this->_productCollection->setPageSize(1);
$this->_productCollection->setCurPage(3);
$this->_productCollection->load();
}
/////////////////////////////////////////////
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}
}
return $this->_productCollection;
}
The important part is to find some way to detect whether you're using the custom list.phtml page or not. Then you'll need to override references to <block type='catalog/product_list' />in the layouts with your class, and you should be set to go.
重要的部分是找到某种方法来检测您是否正在使用自定义 list.phtml 页面。然后你需要<block type='catalog/product_list' />用你的类覆盖布局中的引用,你应该准备好了。
Hope that helps!
希望有帮助!
Thanks, Joe
谢谢,乔
回答by Jongosi
As was mentioned, the productCollectionalready has page size set. There is another way to get the collection though; via the catalog/productModel:
如前所述,productCollection已经设置了页面大小。不过,还有另一种获取集合的方法;通过catalog/product模型:
$productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect(
array('name', 'image', 'price')
)
->addIdFilter(
array('1', '2')
)
->setPageSize( 2 )
->load();
;
return $productCollection->getSelect()->__toString();
The resulting query (and ultimately object) contains the LIMIT syntax:
结果查询(以及最终的object)包含 LIMIT 语法:
SELECT `e`.* ... WHERE (`e`.`entity_id` IN('1', '2')) LIMIT 2;
Is that what you were asking?
这就是你问的吗?

