Php magento限制产品收集调用中返回的项目数
我试图在list.phtml模板的副本中手动限制返回结果的数量,但结果比我预期的要困难得多Php magento限制产品收集调用中返回的项目数,php,collections,magento,pagination,Php,Collections,Magento,Pagination,我试图在list.phtml模板的副本中手动限制返回结果的数量,但结果比我预期的要困难得多 我试着手动设置集合大小,但还是没有效果。有人能告诉我怎么做吗?非常感谢 看起来list.phtml中返回的集合已经调用了load(),这意味着当我们到达模板时,我们已经失去了设置页面大小的机会。所以,这会变得有点混乱 生成该集合的块是Mage\u Catalog\u block\u Product\u List,我们可以用自己的类扩展它,同时覆盖它。创建一个扩展Mage\u Catalog\u block
我试着手动设置集合大小,但还是没有效果。有人能告诉我怎么做吗?非常感谢 看起来list.phtml中返回的集合已经调用了load(),这意味着当我们到达模板时,我们已经失去了设置页面大小的机会。所以,这会变得有点混乱 生成该集合的块是
Mage\u Catalog\u block\u Product\u List
,我们可以用自己的类扩展它,同时覆盖它。创建一个扩展Mage\u Catalog\u block\u Product\u List
的新块,并覆盖方法\u 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;
}
重要的部分是找到某种方法来检测您是否正在使用custom list.phtml页面。然后,您需要在类的布局中覆盖对
的引用,并且应该设置为go
希望有帮助
谢谢,
Joe不幸的是,它不起作用,因为在
\u getProductCollection()
方法中,集合已经用页面大小初始化
一个更灵活的解决方案可能是在事件之前观察catalog\u product\u collection\u load\u,顾名思义,该事件是在集合加载之前调度的
下面是一个示例(假设在yourpackage
下编写yourmodule
扩展):
步骤1:在config.xml中定义观察者
在扩展名文件config.xml
的global
部分插入如下内容:
<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>
希望能有帮助。
真诚地
Alessandro Ronchi一个快速的方法就是使用这种方法。您甚至可以在模板中直接使用它
$_productCollection = clone $this->getLoadedProductCollection();
$_productCollection->clear()
->setPageSize(3)
->load();
@joseph的类似方法是重写Mage\u Catalog\u Block\u Product\u List
,但在新类中插入以下代码:
const PAGE_SIZE = 3;
protected function _getProductCollection(){
$collection = parent::_getProductCollection();
$yourCustomBoolean = someFunctionThatDetectsYourCustomPage();
if($yourCustomBoolean) {
$collection->setPageSize(self::PAGE_SIZE);
}
return $collection;
}
这样,您将从父块继承Mage_目录代码中的任何未来更改,但仍将设置页面限制
理想情况下,您可以使用system.xml
节点创建一个字段,管理员无需硬编码页面大小即可编辑该字段。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>
HTH,
JD我得到了用户代码:clockworkgeek,但这里有一些问题,正确的代码如下,它工作正常,谢谢clockworkgeek
$_productCollection = $this->getLoadedProductCollection();
$_productCollection->clear();
$_productCollection->setPageSize(3)
$_productCollection->load();
您还可以在此处编写输入代码
,或通过修改为解决此问题
$this->getLoadedProductCollection()->clear();
$_productCollection = $this->getLoadedProductCollection();
谢谢,
如果有帮助,请发表评论。如前所述,productCollection
已经设置了页面大小。不过,还有另一种获取收藏的方法;通过目录/产品
型号:
$productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect(
array('name', 'image', 'price')
)
->addIdFilter(
array('1', '2')
)
->setPageSize( 2 )
->load();
;
return $productCollection->getSelect()->__toString();
结果查询(以及最终的对象)包含限制语法:
SELECT `e`.* ... WHERE (`e`.`entity_id` IN('1', '2')) LIMIT 2;
这就是你要问的吗?你是想限制退回产品的数量,以便只显示特定数量的产品,还是出于其他原因?我试图扩展此模块,我的意思是像中一样扩展,甚至只是手动在自定义列表中写入。php页面我正在制作,调用一组产品,以便我可以拥有,来自x类的3个特色产品,来自y类的3个,来自z类的3个,并使其在jquery滑块中显示得非常漂亮。嘿,乔,谢谢你的回复。好的,那么,我可以把它放在custom list.php文件(我用它来创建登录页)中,在其他代码之前,还是我需要把它放在其他地方?我基本上只是扩展这个模块;——允许限制返回的项目数量,这样我就可以创建美观的登录页。我对检测YourCustomPage()的SomeFunction部分有点困惑;--我是否先在landing.php页面上调用该函数,然后再调用其他函数?(很抱歉,我对OO PHP和Zend还是相当陌生的,还有什么不熟悉的。)感谢@Joseph的建议——我在回答中对它做了一些调整,这样就不那么突兀了,而且更能证明未来。这很好,但确实意味着SQL调用要执行两次。请看下面我的回答,以获得另一种选择。@Jonathan-你当然是对的。预防胜于治疗。这很好,只有请您提供一个简短的建议,说明我将如何实现一些检测您的CustomPage()的功能。
?我有一个自定义模块,它使用一个类别页面作为它的基础,我正试图限制该模块的产品数量。但我不知道如何在observer函数中检测到我正在查看该模块的类别页面,而不是站点上的任何其他类别页面。谢谢听起来您应该在模块中使用自定义控制器,而不是(ab)使用类别页面…非常正确,Jonathan。然而,我继承了这个代码库,它太庞大了,现在无法改变。我一直在使用clockworkgeek的解决方案,但肯定更愿意实现您的解决方案;我只是不确定如何检测用户是否在我的特定模块的分类页面上。听起来很有趣:)如果你在@chapReach的这篇博文中看到,这会给你一些提示。另外,检查Mage::registry('current_category')以查看类别控制器是否已经为您将该类别存储在注册表中。
$productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect(
array('name', 'image', 'price')
)
->addIdFilter(
array('1', '2')
)
->setPageSize( 2 )
->load();
;
return $productCollection->getSelect()->__toString();
SELECT `e`.* ... WHERE (`e`.`entity_id` IN('1', '2')) LIMIT 2;