Magento 1 – Fix Limiter Per Page Not Working

magento-1pagerpaginationproduct-list

On Product List page I added a filter Sort By:

  • Newest Products
  • Popularity
  • Name From A-Z
  • Name From Z-A
  • Price – Low to High
  • Price – High to Low

everything is functioning properly

but comes the problem when filter Results per Page (Show / limit how many products per page). I set 12, 24, 36, All.

Before I add Sort by Newest Products, all functioning normally include Show / product limit per page.
Related to Sort by Newest Products, I added this new/custom code below <? Php echo $ this-> getToolbarHtml ()?> to the list of products in the Grid view:

<?php
    if($currentCategoryId){
        $order = Mage::getSingleton('catalog/session')->getSortOrder();
        $direction = Mage::getSingleton('catalog/session')->getSortDirection();
        $display = Mage::getSingleton('catalog/session')->getDisplayMode();
        $limit = Mage::getSingleton('catalog/session')->getLimitPage();

         if($order == 'name') {
            if($direction == '') {
                $direction = 'asc';
            }                
            $_productCollection = Mage::getModel('catalog/category')->load($currentCategoryId)
                 ->getProductCollection()
                 ->addAttributeToSelect('*')
                 ->addAttributeToFilter('status', 1)
                 ->addAttributeToFilter('visibility', 4)                                          
                 ->setOrder('name', $direction)//;
                 ->setPageSize($limit)
                 ->setCurPage($limit);

            //echo $_productCollection->getSelect();
            //$_productCollection->printLogQuery(true);
         }

         if($order == 'price') {
            if($direction == '') {
                $direction = 'asc';
            }
            $_productCollection = Mage::getModel('catalog/category')->load($currentCategoryId)
                 ->getProductCollection()
                 ->addAttributeToSelect('*')
                 ->addAttributeToFilter('status', 1)
                 ->addAttributeToFilter('visibility', 4)
                 ->setOrder('price', $direction)//;
                 ->setPageSize($limit)
                 ->setCurPage($limit);
         }

         if($order == 'position') {
            if($direction == '') {
                $direction = 'asc';
            }
            $_productCollection = Mage::getModel('catalog/category')->load($currentCategoryId)
                 ->getProductCollection()
                 ->addAttributeToSelect('*')
                 ->addAttributeToFilter('status', 1)
                 ->addAttributeToFilter('visibility', 4)
                 ->setOrder('position', $direction)//;
                 ->setPageSize($limit)
                 ->setCurPage($limit);
         }

         if($order == '') {
            if($direction == '') {
                $direction = 'asc';
            }
            $_productCollection = Mage::getModel('catalog/category')->load($currentCategoryId)
                 ->getProductCollection()
                 ->addAttributeToSelect('*')
                 ->addAttributeToFilter('status', 1)
                 ->addAttributeToFilter('visibility', 4)
                 ->setOrder('news_from_date', 'desc')
                 ->setOrder('created_at', 'desc')//;
                 ->setPageSize($limit)
                 ->setCurPage($limit);
        }
    }

?>

The full code (andi\app\design\frontend\rwd\default\template\catalog\product\list.phtml) can be found at Pastebin : http://pastebin.com/U5UpgUmj

Page Limit does not function.
When I click on page 1, it will look like in figure 1.
By the time I click on page 2, it will look like Figure 2 (image Page 2 – Error), where the picture repeats a number of 12. It should be on page 2 (image Page 2 – Should), which appear only 7 product images in page 2 ( from total of 19 products).

Limit Product per Page

I had read this tutorial : https://www.creare.co.uk/blog/magento/limiting-collection-size-in-magento and try to use it.

How to limit per-page to function properly? Where is the error in my code?

Best Answer

Try this

setCurPage() should be current page value

 if($currentCategoryId){
        $order = Mage::getSingleton('catalog/session')->getSortOrder();
        $direction = Mage::getSingleton('catalog/session')->getSortDirection();
        $display = Mage::getSingleton('catalog/session')->getDisplayMode();
        $limit = Mage::getSingleton('catalog/session')->getLimitPage();
        // Default name for page number is p

$currentPage = (int) ($this->getRequest()->getParam('p')) ? $this->getRequest()->getParam('p') : 1;

         if($order == 'name') {
            if($direction == '') {
                $direction = 'asc';
            }                
            $_productCollection = Mage::getModel('catalog/category')->load($currentCategoryId)
                 ->getProductCollection()
                 ->addAttributeToSelect('*')
                 ->addAttributeToFilter('status', 1)
                 ->addAttributeToFilter('visibility', 4)                                          
                 ->setOrder('name', $direction)//;
                 ->setPageSize($limit)
                 ->setCurPage($currentPage);

            //echo $_productCollection->getSelect();
            //$_productCollection->printLogQuery(true);
         }

         if($order == 'price') {
            if($direction == '') {
                $direction = 'asc';
            }
            $_productCollection = Mage::getModel('catalog/category')->load($currentCategoryId)
                 ->getProductCollection()
                 ->addAttributeToSelect('*')
                 ->addAttributeToFilter('status', 1)
                 ->addAttributeToFilter('visibility', 4)
                 ->setOrder('price', $direction)//;
                 ->setPageSize($limit)
                 ->setCurPage($currentPage);
         }

         if($order == 'position') {
            if($direction == '') {
                $direction = 'asc';
            }
            $_productCollection = Mage::getModel('catalog/category')->load($currentCategoryId)
                 ->getProductCollection()
                 ->addAttributeToSelect('*')
                 ->addAttributeToFilter('status', 1)
                 ->addAttributeToFilter('visibility', 4)
                 ->setOrder('position', $direction)//;
                 ->setPageSize($limit)
                 ->setCurPage($currentPage);
         }

         if($order == '') {
            if($direction == '') {
                $direction = 'asc';
            }
            $_productCollection = Mage::getModel('catalog/category')->load($currentCategoryId)
                 ->getProductCollection()
                 ->addAttributeToSelect('*')
                 ->addAttributeToFilter('status', 1)
                 ->addAttributeToFilter('visibility', 4)
                 ->setOrder('news_from_date', 'desc')
                 ->setOrder('created_at', 'desc')//;
                 ->setPageSize($limit)
                 ->setCurPage($currentPage);
        }
    }