Unable to limit product collection size in Magento 1.9.2.3 with custom module












0















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);









share|improve this question



























    0















    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);









    share|improve this question

























      0












      0








      0


      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);









      share|improve this question














      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);






      magento-1.9 collection






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 '16 at 9:15









      ShanShan

      248421




      248421






















          3 Answers
          3






          active

          oldest

          votes


















          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);






          share|improve this answer
























          • 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



















          2














          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.






          share|improve this answer


























          • 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);





          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f108126%2funable-to-limit-product-collection-size-in-magento-1-9-2-3-with-custom-module%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            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);






            share|improve this answer
























            • 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














            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);






            share|improve this answer
























            • 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












            3








            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);






            share|improve this answer













            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);







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 27 '16 at 11:21









            simonthesorcerersimonthesorcerer

            3,48221126




            3,48221126













            • 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



















            • 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

















            performance issue !! let me wait for more comments

            – Shan
            Mar 27 '16 at 12:25





            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





            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













            2














            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.






            share|improve this answer


























            • 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
















            2














            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.






            share|improve this answer


























            • 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














            2












            2








            2







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 6 mins ago









            Teja Bhagavan Kollepara

            2,94841847




            2,94841847










            answered Mar 27 '16 at 9:22









            Ajay PatelAjay Patel

            9491427




            9491427













            • 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



















            • 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

















            i tried these already before. but seems to be the same :(

            – Shan
            Mar 27 '16 at 9:54





            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





            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





            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);





            share|improve this answer




























              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);





              share|improve this answer


























                1












                1








                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);





                share|improve this answer













                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);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 6 '16 at 8:47









                Fabian SchmenglerFabian Schmengler

                54.5k20131343




                54.5k20131343






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f108126%2funable-to-limit-product-collection-size-in-magento-1-9-2-3-with-custom-module%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What other Star Trek series did the main TNG cast show up in?

                    Berlina muro

                    Berlina aerponto