How to limit the number of result within Magento?

I’ve a recent products action in my Catalog/Product controllers.

I retrieve all the product sorted by entity id. Works great ok.

The problem is I just want to show 20 products in 4 pages.

I tried to extend Mage_Catalog_Block_Product_List and override _getProductCollection() and I did something like this:

$this->_productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSort('entity_id', 'desc')
                ->addStoreFilter();

And the important part:

$this->_productCollection->getSelect()->limit($this->getProductsLimit());

If I ‘dump’ the returned Object I’ve something like this:

[“limitcount”] => int(5)
[“limitoffset”] => int(0)

So it looks to be overriden by the paginator.

Do you know a way to limit properly the number of result?

For both optimization and presentation I actually don’t want to retrieve all the products collection.

Thank you

5 thoughts on “How to limit the number of result within Magento?”

  1. That should work…

    $this->_productCollection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSort('entity_id', 'desc')
                    ->addStoreFilter()
                    ->setPage($pageNum, $pageSize);
    
    // only retrieve 10 products
    $this->_productCollection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSort('entity_id', 'desc')
                    ->addStoreFilter()
                    ->setPage(0, 10);
    

  2. Select 20 products:-

    $this->_productCollection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSort('entity_id', 'desc')
                    ->addStoreFilter()
                    ->setPageSize(20);
    

Leave a Reply

Your email address will not be published. Required fields are marked *