10

For my custom module I have get product by manufacturer. For template I have copied list.phtml.

On the template file pagination appears but it shows all product instead of selected limit per page. Sorting is not working too.

How can I make it work??

It is my block file:

protected function _getProductCollection() 
{
    if (is_null($this->_productCollection)) {
        $layer = $this->getLayer();
        $brand_id = $this->getRequest()->getParam('id');
        $collection = Mage::getModel('catalog/product')->getCollection();
        $collection->addAttributeToSelect('*');
        $collection->addFieldToFilter(array(
            array('attribute' => 'manufacturer', 'eq' => $brand_id)
        ));
    }

    return $collection;
}

5 Answers 5

2

Use below snippet to add pagination and sorting on your custom collection. For every custom collection listing you have to create custom toolbar pager as well.

    $itemsLimit         =   $_GET["limit"] ? $_GET["limit"] : Mage::getStoreConfig('catalog/frontend/grid_per_page');   //Set items to show per page
    $currPage               =   $_GET["p"] ? $_GET["p"] : 1;                //Set current page      
   /*   Set Pagination for Custom Loaded Collection */                              
    $toolbar = Mage::getBlockSingleton('catalog/product_list')->getToolbarBlock();
    $toolbar->setCollection($_productCollection);

    /*  Set Pager   */
    $pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
    $pager->setAvailableLimit(array($itemsLimit=>$itemsLimit));
    $pager->setCollection($_productCollection);
    $toolbar->setChild('product_list_toolbar_pager', $pager);
    $toolbar->setData('_current_limit', $itemsLimit);

After this, replace

$this->getToolbarHtml(); by $toolbar->toHtml(); 

to display bottom pager and top sorting toolbar.

For sorting order, do this before collection load:

$_productCollection->addAttributeToSort($_GET["order"], $_GET["dir"]');

I hope this resolves your problem.

1
  • also you can add $_productCollection->setPageSize($itemsLimit) ->setCurPage($currPage); Commented Jun 2, 2016 at 12:35
1

You have to use following filters too:

    ->addAttributeToSort($_GET['order'],$_GET['dir'] )
    ->setPageSize($limit)
    ->setCurPage($_GET['p'])

So your complete code becomes:

    protected function _getProductCollection() 
    {
        if (is_null($this->_productCollection)) 
        {
            $layer = $this->getLayer();
            $brand_id = $this->getRequest()->getParam('id');
            $collection = Mage::getModel('catalog/product')->getCollection();
            $collection->addAttributeToSelect('*');
            $collection->addFieldToFilter(array(array('attribute'=>'manufacturer','eq'=>$brand_id),
    ))
$collection->addAttributeToSort($_GET['order'],$_GET['dir'] );
    $collection->setPageSize($_GET['limit']);
    $collection->setCurPage($_GET['p']);



        }

        return $collection;
    }
1

This may not be the situation in your case, but it might help someone else who is having this problem. I had this problem while testing custom changes in getProductCollection().

I was able to fix it by removing logging code I had added that noted the count of the returned collection. I believe that anything that interrogates the Collection load results in the getProductCollection() method will force the collection to actually load its products prematurely and will prevent downstream modifications to the collection ordering and result limitation from being applied for the paging controls.

2
  • 1
    To still be able to log the collection count without loading the collection items, use $collection->getSize(). It will perform a separate SELECT COUNT(*) with current collection filters. Commented Jan 24, 2016 at 9:14
  • Thanks @Jan Papenbrock. That is handy. The larger point I was trying to make though remains. If you cause the collection to load, it will interfere with paging and sorting which are applied to the collection later if it has not been loaded.
    – Matt B
    Commented Jan 25, 2016 at 21:19
0

You have to override the _prepareLayout() and set the data as below snap.

protected function _prepareLayout()
{
    parent::_prepareLayout();
    $pager = $this->getLayout()->createBlock('page/html_pager')->setCollection($this->getDatasets());
    $this->setChild('pager', $pager);
    $this->getDatasets()->load();
    return $this;
}

let me know if you have any query.

2
  • it still not working. after adding this it displays white screen.
    – Piyush
    Commented Jan 20, 2015 at 9:20
  • it will not just like copy and paste buddy
    – Keyul Shah
    Commented Jan 20, 2015 at 9:25
0

Go to Manage Category Section and set is Anchor Option to "Yes".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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