Magento: a collection of filtered filters

I want to filter and paginate a collection of products. everything is fine - except pagination. im just returning the entire collection, not 3 elements for the first page.

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

thanks for your support!

+5
source share
2 answers

Thanks @Ben! You gave me the right hint. Now it works! Basically, I create another collection and filter it using the identifiers of the already filtered items. After that, it's easy to add pagination to this new collection. What is the working code:

    //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 />';
    }
+4
source

You are so close! Try moving this line $product_collection->setPageSize(3)->setCurPage(1);before the first iteration foreach()over the collection.

Magento . , load() ( count() foreach()), , ( EDIT: . ). , , _items.

FYI clear(), , (, , , ..), .

EDIT. , _items, , .

+13

All Articles