Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Limit results collection
 
CBC
Member
 
Total Posts:  48
Joined:  2008-01-15
Spain
 

I have a class that show the last products.

I am using a class with method _getProductCollection. In this method I have:

$collection = Mage::getResourceModel(’catalog/product_collection’);
Mage::getModel(’catalog/layer’)->prepareProductCollection($collection);
$collection->setPage(1, 4);

but it’s not limit the results.

Somewhere can help me?

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Daim
Sr. Member
 
Total Posts:  172
Joined:  2008-01-08
Germany
 

Use ->setPageSize(10)

 
Magento Community Magento Community
Magento Community
Magento Community
 
CBC
Member
 
Total Posts:  48
Joined:  2008-01-15
Spain
 

Thanks, but
$collection->setPageSize(10)
don’t work neither.

Always it show me the same product’s number.

 
Magento Community Magento Community
Magento Community
Magento Community
 
CBC
Member
 
Total Posts:  48
Joined:  2008-01-15
Spain
 

I has the solution…

It’s cause by block pagination, you can not load block pagination if you can not display it.

For limit use $collection->setPageSize(4);

Thanks for all.

 
Magento Community Magento Community
Magento Community
Magento Community
 
firewizard
Member
 
Avatar
Total Posts:  43
Joined:  2007-11-22
Bucharest, Romania
 

just in case anyone bumps into the same problem (although the post is one year old). try

$collection->getSelect()->limit)
this should go before loading the collection.
 
Magento Community Magento Community
Magento Community
Magento Community
 
Rinku
Member
 
Total Posts:  41
Joined:  2008-09-17
New Delhi
 

Hello All,

I have a problem, I have approx 1500 records in product collection. But I want only 100 products to show. Kindly let me know how can I set limit in product collection.

here is my code.

$this->_productCollection = Mage::getResourceModel(’reports/product_collection’)
->setStoreId($storeId)
->addAttributeToFilter(’visibility’,$visibility)
->addAttributeToSelect(’*’)
->addViewsCount()
->joinAttribute(’status’, ‘catalog_product/status’, ‘entity_id’, null, ‘inner’)
->addAttributeToFilter(’status’,1);

Thank you.
Sandeep

 
Magento Community Magento Community
Magento Community
Magento Community
 
JackieZhou
Jr. Member
 
Total Posts:  4
Joined:  2009-02-06
 

You all need to call ‘setPage(pageNum, pageCount)’ method by collection object.

$categoryCollections = Mage::getModel(’catalog/category’)
->getCollection()
->addAttributeToFilter("parent_id", $defaultCategoryId)
->setPage(1, 4)
->load();

The code above mean that we get the top 4 categories from current store.

jacky

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jing Jing
Jr. Member
 
Total Posts:  2
Joined:  2010-01-05
 

We want result limit and pagination as well. So we implement as below.

copy

app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php

to

app/code/local/Mage/Eav/Model/Entity/Collection/Abstract.php

add a member

protected $_maxSize null;

add methods for max size

public function setMaxSize($maxSize){
        $maxSize 
intval($maxSize);
        
$this->_maxSize $maxSize $maxSize 0;
        return 
$this;
    
}
    
    
public function getMaxSize(){
        
return $this->_maxSize;
    
}

overwrite getSize() for max size

public function getSize(){
        $size 
parent::getSize();
        
$max_size $this->getMaxSize();
        if (!empty(
$max_size)) {
            
return $max_size $size $max_size $size;
        

        
        
return $size;
    
}

in method _loadEntities, update from

if ($this->_pageSize{
                $this
->getSelect()->limitPage($this->getCurPage(), $this->_pageSize);
        
}
to
if ($this->_pageSize{
            
if (!empty($this->_maxSize
                && 
$this->getCurPage() == $this->getLastPageNumber() 
                   && (
$this->_maxSize $this->_pageSize 0) ) {
                       $this
->getSelect()->limit($this->_maxSize $this->_pageSize, ($this->getCurPage() - 1) * $this->_pageSize);   
            
}
            else{
                $this
->getSelect()->limitPage($this->getCurPage(), $this->_pageSize);
            
}
        }

Now you can set max size to limit your results collection by

$someCollection->setMaxSize(100);
 
Magento Community Magento Community
Magento Community
Magento Community
 
kaushal_kk
Sr. Member
 
Total Posts:  83
Joined:  2011-06-28
 

Hi All, I want to get the Loaded Products (Means Search Result) in another file so what should I do?

For example - I search “Ring” through Quick Search then result is coming it is all ok but how can I get that result (Means All The Products Coming Through Search) in another file.

Actually I want to display each & every product’s (Means All The Products Coming Through Search) category in leftbar of that page (Means Search Page).

I am finding an appropriate way for this but for now I made a patch that - Just made a new file & placed it in CatalogSearch folder. In xml file of catalogsearch I took that file in leftbar like this :-

<block type="catalogsearch/result" name="search_result_list2" template="catalogsearch/ProductWiseCategories.phtml"></block>

I got what I wanted but now I am facing another thing that whatever category comes to leftbar through my logic that is only for those products which are loaded in current page but I want all.

my Logic for getting products when search occur :

$cat = array();
$_productCollection $this->_getProductCollection()->setPage(0,300);

foreach(
$_productCollection->getItems() as $_product):
    
$_categories $_product->getCategoryIds();
    
$_category Mage::getModel('catalog/category')->load($_categories[0]);
    
$url $_category->getUrl();
    
$cat[] "<li><a href='".$url."'>".$_category->getName()."</a></li>";
endforeach;
            
echo 
implode(" ",array_unique($cat));

So problems are 2
1) Though I have got the collection of all the products loaded through search but need appropriate way for this because may be what I have done is not proper.
2) I need to get all the products(Means All The Products Coming Through Search) not only of current page. (Important is this for now to me) I have already used 1)setPage() 2)setPageSize & also getSize(). Like 1)$_productCollection = $this->_getProductCollection()->setPage(0,300); 2) $_productCollection->setPageSize(100); but nothing is working.

So Please Help.

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Kaushik Patel
Jr. Member
 
Total Posts:  28
Joined:  2011-12-24
 

Try this two line of code.
working like a charm.....

http://hkpatel201.blogspot.in/2013/02/magento-set-page-limit.html

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ricardo_Martins
Member
 
Avatar
Total Posts:  48
Joined:  2009-02-04
Santos, SP, Brazil
 

So, just use the clear() method…

$_productCollection=$this->getProductCollection()
                        ->
clear()
                        ->
setPageSize(100)
                        ->
load();

The clear method informs the Varien Collection that it is not loaded yet, so you can apply the setPage, etc again.. as you can see in Varien/Data/Collection.php…

/**
     * Clear collection
     *
     * @return Varien_Data_Collection
     */
    public function clear()
    
{
        $this
->_setIsLoaded(false);
        
$this->_items = array();
        return 
$this;
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
samil
Jr. Member
 
Total Posts:  4
Joined:  2009-09-22
 

I am using this function with in custom module block that overrides core block

$collection parent::_getProductCollection()->clear()->setPageSize(3) ->load();

But the pagination is not getting updated accordingly

I have 12 products in that collection, but in pagination it shows only 1,2 pages the item per page count being 9

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top