开发者

How to limit the number of result within Magento?

开发者 https://www.devze.com 2023-02-16 18:18 出处:网络
I\'ve a recent products action in my Catalog/Product controllers. I retrieve all the product sorted by entity id. Works great ok.

I've a recent products action in my Catalog/Product controllers.

I retrieve all the product sorted by entity id. Works great ok.

The problem is I just want to show 20 products in 4 pages.

I tried to extend Mage_Catalog_Block_Product_List and override _getProductCollection() and I did som开发者_运维百科ething like this:

$this->_productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSort('entity_id', 'desc')
                ->addStoreFilter();

And the important part:

$this->_productCollection->getSelect()->limit($this->getProductsLimit());

If I 'dump' the returned Object I've something like this:

["limitcount"] => int(5) ["limitoffset"] => int(0)

So it looks to be overriden by the paginator.

Do you know a way to limit properly the number of result?

For both optimization and presentation I actually don't want to retrieve all the products collection.

Thank you


That should work...

$this->_productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSort('entity_id', 'desc')
                ->addStoreFilter()
                ->setPage($pageNum, $pageSize);

// only retrieve 10 products
$this->_productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSort('entity_id', 'desc')
                ->addStoreFilter()
                ->setPage(0, 10);


Try this

$this->_productCollection = Mage::getModel('catalog/product')->getCollection() ->setPageSize(20) ->setCurPage(1);


Another, equally valid, way is with:

$this->_productCollection->setPageSize($this->getProductsLimit());


Select 20 products:-

$this->_productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSort('entity_id', 'desc')
                ->addStoreFilter()
                ->setPageSize(20);


Try this after obtaining the collection:

$this->_productCollection->getSelect()->limit( 20 );

0

精彩评论

暂无评论...
验证码 换一张
取 消