Elamurugan
Elamurugan

Reputation: 3214

Magento Pagination bar Limiting the number of items

In magento ,I need to load only top 20 items from product collections with pagination bar(pagination bar should only have 4 pages because its only have 20 items - But in my case its having total product collection because magento pagination bar is taking whole collection object for creating toolbar without limit property).

I have the following code

    $_productCollection = Mage::getResourceModel('reports/product_collection')
          ->addAttributeToSelect('*')
          ->addOrderedQty()
          ->setPageSize($limit)
          ->setPage($p, $limit)     
          ->setOrder('ordered_qty', 'desc');

And this returned 20 items correctly. When i set this to pagination bar by following method.

$magento_block = Mage::getSingleton('core/layout');
    $productsHtml  = $magento_block->createBlock('catalog/product_list');
    $productsHtml  ->setTemplate('catalog/product/list.phtml')->setCollection($_productCollection);
    $pager         = $magento_block->createBlock('page/html_pager', 'catalog/product_list')->setCollection($_productCollection);
    $productsHtml->setChild('pager', $pager);

Its taking all the product collection instead of just 20 items for creating toolbar. So i added a where condition in the productcollection itself as follows.

$_productCollection->getSelect()->where("order_items.qty_ordered  >= 50");

This collection returns items which are sold more than 50 quantities. But what i want is exactly 20 items with correct pagination bar.

How can i setLimit in pagination bar.

The following also does not worked for me

$magento_block->createBlock('page/html_pager', 'catalog/product_list')->setCollection($_productCollection)->setLimit(20);

Note: Am loading this product collection outside Magento Enterprise 1.9 version and not with in magento template. Can someone find whats the mistake am doing here?

Upvotes: 2

Views: 3830

Answers (1)

Oğuz Çelikdemir
Oğuz Çelikdemir

Reputation: 4980

I am not sure ( it seems to me it's not loading your $_productCollection in the second code ) but I guess you had forgetten to load collection.

$model = Mage::getResourceModel('reports/product_collection');
$collection = $model->getCollection();
$collection->AddAttributeToSelect(*);
$collection->AddOrderedQty();
$collection->SetPageSize($limit);
$collection->load();

Upvotes: 0

Related Questions