1

I am using a custom module developed by third party, and i have probem with limiting product collections on my home page by :

$collection->setPageSize(10); 

But the limit will work fine if switch the pager from

20 products per page to show all

The code is :

$collection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($category);
    $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
    $collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
    $collection = $this->_addProductAttributesAndPrices($collection)->addStoreFilter()
                        ->addAttributeToFilter('is_deal', 1)
                        ->addAttributeToFilter('deal_approved', 1)
                        ->addAttributeToFilter('deal_type',array(
                                array('attribute'=>'deal_type','neq'=>  AdolMedia_DailyDeals_Model_Product_Attribute_Source_Dealtype::FEATURED_DEAL )
                        ))
                        ->addFieldToFilter('deal_cities',array(
                                array('attribute'=>'deal_cities','finset'=> $current_city),
                                array('attribute'=>'deal_cities', 'eq'=> 0),
                                array('attribute'=>'deal_cities', 'eq'=>  new Zend_Db_Expr('null'))
                        ),'left')
                        ->addAttributeToFilter('deal_from_date', array('or'=> array(
                            0 => array('date' => true, 'to' => $dealTime),
                            1 => array('is' => new Zend_Db_Expr('null')))
                        ), 'left')
                        ->addAttributeToFilter('deal_to_date', array('or'=> array(
                            0 => array('date' => true, 'from' => $dealTime),
                            1 => array('is' => new Zend_Db_Expr('null')))
                        ), 'left')
                        ->addAttributeToSort('deal_from_date', 'desc');
    $collection->setPageSize(10); // not working  
    $this->setProductCollection($collection);
3

I had this problem sometimes when I did not realize that the collection had already been loaded when I tried to set the page size.

So, you could try to change the order of your function calls: use $collection->getSelect()->limit(20); or $collection->setPageSize(10); directly after $collection->getSelect()->order(new Zend_Db_Expr('RAND()'));.

Another possibility is to call the clear() method on the collection; this will reset the $_isLoaded flag, and will force a new database request - be careful with performance issues here, though. So the code here would be:

$collection->clear(); $collection->setPageSize(10);

  • performance issue !! let me wait for more comments – Shan Mar 27 '16 at 12:25
  • Thanks bro, after struggling for 3 hours found your answer. I am not able to reset the collection order because of load function. – Amit Singh Jan 6 '18 at 14:29
3

There are several ways to do this:

$collection = Mage::getModel('...')
            ->getCollection()
            ->setPageSize(20)
            ->setCurPage(1);

Will get first 20 records.

Here is the alternative and maybe more readable way:

$collection = Mage::getModel('...')->getCollection();
$collection->getSelect()->limit(20);

This will call Zend Db limit. You can set offset as second parameter.

  • i tried these already before. but seems to be the same :( – Shan Mar 27 '16 at 9:54
  • what is result?? – Ajay Patel Mar 27 '16 at 10:03
  • the collection is returning default number of product (back end value for >> Products per Page on Grid Default Value << ) these code have no effect at all. this will work only if i switch 'show x product' drop down on pager toolbar to 'All' – Shan Mar 27 '16 at 12:24
1

From your description, I assume that $this is a product list block that uses the pager toolbar. The toolbar block applies the page size limit to the collection based on default value and session variable or request parameter. It does not apply any limit if "show all" is selected, that's why your value is not overridden in this case.

You can set the limit directly on the toolbar block to bypass the normal calculation.

Replace

$collection->setPageSize(10);

with

$this->getToolbarBlock()->setData('_current_limit', 10);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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