2012-02-03 178 views
5

我想过滤和分页产品集合。一切都很好 - 除了分页。即时通讯只是让整个收集,而不是第一页的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 />'; 
    } 

感谢您的支持!

回答

4

Thanks @Ben!你给了我正确的提示。现在它工作!基本上我正在创建另一个集合,并通过已经过滤的项目的id筛选这个集合。之后,它很容易添加到新的集合分页。这是工作代码:

//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); 
     } 
    } 

    //build id array out of filtered items (NEW!) 
    foreach($product_collection as $item){ 
     $arrProductIds[]=$item->getId(); 
    } 

    //recreate collection out of product ids (NEW) 
    $product_filtered_collection = Mage::getModel('catalog/product')->getCollection(); 
    $product_filtered_collection->addAttributeToFilter('entity_id', array('in'=>$arrProductIds)); 


    //add pagination (on new collection) (NEW) 
    $product_filtered_collection->setPageSize(3)->setCurPage(1); 


    //TEST OUTPUT 
    foreach ($product_filtered_collection as $product) { 
      echo $product->getName().'<br />'; 
    } 
13

你太亲近了!尝试移动$product_collection->setPageSize(3)->setCurPage(1);之前第一个foreach()迭代集合。

Magento的集合是延迟加载。直到您直接load()(或通过调用count()foreach()隐式加载它们),您可以修改影响基础查询的集合属性(EDIT:请参阅下面的注释)。一旦集合被明确或隐式加载,尽管您只会获得已设置的_items属性的成员。

仅供参考,您可以拨打clear()以保留原始的影响查询的属性(过滤器,分类器,限制,连接等),然后添加其他属性。

HTH

编辑:其实,调整查询性能始终是可能的,无论_items负荷状态,但直到收集再生的效果将不可见。

+0

到目前为止感谢!我尝试重新生成集合: '$ product_filtered_collection = new Varien_Data_Collection(); foreach($ product_collection as $ item){ $ product_filtered_collection-> addItem($ item); } '...并添加分页到这个新的集合 但是,这并不工作要么 – 2012-02-03 15:29:23

+0

唉。我只是重新阅读你的文章。您将要解决两个更大的问题。为了分类您的收藏,价格过滤需要成为原始查询加载数据的一部分(不是这样的,因为'foreach()'首先加载数据。另外,根据您的需要,最终价格逻辑在Magento中可能会受到观察者的影响,这些观察者不会为这个集合中的项目运行。对不起! – benmarks 2012-02-03 16:35:32

+0

是的@Ben!你指出我正确的方向,现在它工作,我会在约4小时后发布工作示例我的学分太低) 基本上它是关于创建一个新的集合,通过产品的id来过滤它,然后对它进行分页。 – 2012-02-03 17:26:52