7

I am trying to show different number of products in category but it is only working on page 1. I want to show 22 products on page1 and rest all the pages 20 products per page. I managed to change by setPageSize in first page and it is working well, but rest all the pages I am unable to get products limit to 20 per page.

Page 1...22 products //works well
Page 2...20 products //doesn't work..shows from 21 to 40
Page 3...20 products //doesn't work..shows from 41 to 60

Here is how I am doing:

$currentPage = (int) Mage::app()->getRequest()->getParam('p');
if ($currentPage==0 || $currentPage==1){
    $productCollection->clear();
    $productCollection->setCurPage($currentPage)->setPageSize(22); //works..
} else {
        $productCollection->clear(); 
        $productCollection->getSelect()->limit(20,22); //doesn't work
}

Any idea how can I solve this?

Thanks.

4
+50

The problem is that the LIMIT is calculated based on "page size" and "current page" immediately before the collection is loaded. See Mage_Eav_Model_Entity_Collection_Abstract::_loadEntities():

public function _loadEntities($printQuery = false, $logQuery = false)
{
    if ($this->_pageSize) {
        $this->getSelect()->limitPage($this->getCurPage(), $this->_pageSize);
    }

You can prevent this by setting _pageSize to 0:

$productCollection->setPageSize(0);
$productCollection->getSelect()->limit(20,22);

Besides that, resetting the already loaded collection to load it again is not a good idea.

You can write an observer for catalog_product_collection_load_before to manipulate the limit right before the collection is loaded:

public function setProductPaging(Varien_Event_Observer $observer) 
{
    $collection = $observer->getCollection();
    if ($collection->getCurPage() == 1) {
        $collection->setPageSize(22);
    } else {
        $productCollection->setPageSize(0);
        $productCollection->getSelect()->limit(20, 22 + 20 * ($collection->getCurPage() - 2));
    }
}
  • Thanks! I will check this and let you know if it works. Looks like it should work though. – Kalpesh Sep 29 '15 at 20:28
  • It does seem to work but pagination is not correct. If there are 21 products then instead of showing 1 page it shows 2. Any idea? – Kalpesh Sep 30 '15 at 18:09
  • OK this is going to be tricky because the number of pages is calculated by number of items divided by page size. You will need to manipulate this calculation in the toolbar block somehow – Fabian Schmengler Sep 30 '15 at 18:29
  • I figured it out we can set the product limit on cat page here Mage::getSingleton('catalog/session')->setLimitPage(22); before toolbar is called. Thanks! – Kalpesh Sep 30 '15 at 21:33
  • I have used this event catalog_block_product_list_collection – Kalpesh Sep 30 '15 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.