我想过滤和分页产品集合。一切都很好 - 除了分页。即时通讯只是让整个收集,而不是第一页的3个项目。Magento:paginate过滤产品集合
//fetch all visible products
$product_collection = Mage::getModel('catalog/product')->getCollection();
//set wanted fields (nescessary for filter)
$product_collection->addAttributeToSelect('name');
$product_collection->addAttributeToSelect('description');
$product_collection->addAttributeToSelect('price');
$product_collection->addAttributeToFilter('visibility', array('neq' => 1));
//filter by name or description
$product_collection->addFieldToFilter(array(
array('attribute'=>'name','like'=>$sTerm),
array('attribute'=>'description','like'=>$sTerm)
));
//filter for max price
foreach ($product_collection as $key => $item) {
if($item->getPrice() >= $priceTo){
$product_collection->removeItemByKey($key);
}
}
//pagination (THIS DOESNT WORK!)
$product_collection->setPageSize(3)->setCurPage(1);
//TEST OUTPUT
foreach ($product_collection as $product) {
echo $product->getName().'<br />';
}
感谢您的支持!
到目前为止感谢!我尝试重新生成集合: '$ product_filtered_collection = new Varien_Data_Collection(); foreach($ product_collection as $ item){ $ product_filtered_collection-> addItem($ item); } '...并添加分页到这个新的集合 但是,这并不工作要么 – 2012-02-03 15:29:23
唉。我只是重新阅读你的文章。您将要解决两个更大的问题。为了分类您的收藏,价格过滤需要成为原始查询加载数据的一部分(不是这样的,因为'foreach()'首先加载数据。另外,根据您的需要,最终价格逻辑在Magento中可能会受到观察者的影响,这些观察者不会为这个集合中的项目运行。对不起! – benmarks 2012-02-03 16:35:32
是的@Ben!你指出我正确的方向,现在它工作,我会在约4小时后发布工作示例我的学分太低) 基本上它是关于创建一个新的集合,通过产品的id来过滤它,然后对它进行分页。 – 2012-02-03 17:26:52