cancel
Showing results for 
Search instead for 
Did you mean: 

Apply limit on product collection retrieve from database

Apply limit on product collection retrieve from database

Hello,

I am using Magento Enterprise edition 1.13.02. On a product listing page, I want to restrict the number of products fetch from database. For example, if particular category has 100 products, I want to set a limit of 50 and display only 50 products and toolbar and pagination should work as per 50 products count. Want to set a limit not on per page, but on whole collection where only 50 records will be fetched from database.

 

Did anyone get a chance to work on this and make it work successfully ?

2 REPLIES 2

Re: Apply limit on product collection retrieve from database

There are several ways to do this:

$collection = Mage::getModel('...')
            ->getCollection()
            ->setPageSize(50)
            ->setCurPage(1);

Will get first 50 records.

Here is the alternative and maybe more readable way:

$collection = Mage::getModel('...')->getCollection();$collection->getSelect()->limit(50);

This will call Zend Db limit. You can set offset as second parameter.

 

If my answer is useful, please Accept as Solution & give Kudos

Magento Developer
Ankita Biswas

Re: Apply limit on product collection retrieve from database

Hi Ankita,

 

Thanks for reply.

Actually, Limit gets set by functions setPageSize() and limit(). Also limit is set on query and records we get are 50 from raw query

But in real time, Magento pagination works according to total products only, say 100.

I display 10 products per page, so total page numbers are 10 for 100 products. So after applying your solution and setting limit 50, total page number should be 5. But its not working like that, Magento loads a full collection and pagination shows 10 pages. However, as per our limit, only 50 products show but after 5 pages, products gets repeated like previous 5 pages. So on first 5 pages, first 50 products and on next 5 pages, same 50 products show.