0

UPDATE: this is partially answered(for someone who comes here lately).The answers does not updated the search filters.

I want to limit my product collection in any category/subcategory landing page with the help of a url parameter say itemlimit=10

I tried to do this in my phtml with setPageSize(3) but this is setting the number of product in a page so the filters wont get modified accordingly.

Then i tried to do it with

$collection->getSelect()->limit(10);

and getting an error like this

Unrecognized method 'setCurPage()'

Am I doing the wrong thing? Should I override the catalog resource model collection for this?

EDIT:

This is my custom block class

class Ziva_Customcatalog_Block_Product_List extends Mage_Catalog_Block_Product_List {
    public function __construct() {
        parent::__construct();
    }

    protected function _getProductCollection() {
        $collection = Mage::getModel('catalog/product')->getCollection();
        return $collection->getSelect()->limit(10); // This will first 10 results    
    }
}

EDIT as asked: the entire trace

#0 C:\wamp\www\magento2\app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php(225): Zend_Db_Select->__call('setCurPage', Array)
#1 C:\wamp\www\magento2\app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php(225): Varien_Db_Select->setCurPage(1)
#2 C:\wamp\www\magento2\app\code\core\Mage\Catalog\Block\Product\List.php(158): Mage_Catalog_Block_Product_List_Toolbar->setCollection(Object(Varien_Db_Select))
#3 C:\wamp\www\magento2\app\code\core\Mage\Core\Block\Abstract.php(862): Mage_Catalog_Block_Product_List->_beforeToHtml()
#4 C:\wamp\www\magento2\app\code\core\Mage\Core\Block\Abstract.php(582): Mage_Core_Block_Abstract->toHtml()

.................................................. #27 C:\wamp\www\magento2\app\code\core\Mage\Core\Model\App.php(354): Mage_Core_Controller_Varien_Front->dispatch() #28 C:\wamp\www\magento2\app\Mage.php(683): Mage_Core_Model_App->run(Array) #29 C:\wamp\www\magento2\index.php(87): Mage::run('', 'store') #30 {main}

1
  • Did you find solution of this error? I am also stuck with same error.
    – Mohit
    Aug 10, 2017 at 14:26

2 Answers 2

2

Try this : Updated answer

config.xml

<config>
 . . . . . 
<global>  
        <blocks>
            <home>
                <class>Test_Home_Block</class>
            </home>
            <catalog>
                <rewrite>
                    <product_list>Test_Home_Block_Product_List</product_list>
                </rewrite>
            </catalog>
        </blocks> 
 . . . . . .
</global>
 . . . . . 
</config>

~\app\code\local\Test\Home\Block\Product\List.php

<?php
class Test_Home_Block_Product_List extends Mage_Catalog_Block_Product_List
{
     protected function _getProductCollection()
    {
        $collection = parent::_getProductCollection(); 
        $noOfItems = 5; // The number of items which you want to show.
        $count = 0;
        $myProductCollection = new Varien_Data_Collection();
        foreach($collection as $product)
        {
            if($count < $noOfItems) {
                $myProductCollection->addItem($product);
                $count++;
            } else {
                break;
            }
        }

        return $myProductCollection;
    }
}
8
  • This i already tried , please see my question. I have posted the error also there in question
    – zamil
    Nov 14, 2013 at 7:18
  • Sorry. I didn't notice it. Can you put the entire error message here?
    – Sukeshini
    Nov 14, 2013 at 7:38
  • edited my qustion
    – zamil
    Nov 14, 2013 at 8:31
  • Edited the answer as well.
    – Sukeshini
    Nov 14, 2013 at 9:04
  • I tried you updated answer. but i am getting this error Fatal error: Call to a member function setCurPage() on a non-object in C:\wamp\www\magento2\app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php on line 225
    – zamil
    Nov 14, 2013 at 9:30
2

If you only want to filter category_view you could listen to the event which is triggered just before the collection gets loaded. In your config.xmls global section:

<events>
    <catalog_block_product_list_collection>
        <observers>
            <ziva_customcatalog_catalog_block_product_list_collection>
                <class>ziva_customcatalog/observer</class>
                <method>catalogBlockProductListCollection</method>
            </ziva_customcatalog_catalog_block_product_list_collection>
        </observers>
    </catalog_block_product_list_collection>
</events>

In your Observer model you can alter the collection:

/**
 * @param Varien_Event_Observer $observer
 * @return Ziva_Customcatalog_Model_Observer
 */
public function catalogBlockProductListCollection(Varien_Event_Observer $observer)
{
    $collection = $observer->getCollection();
    if($collection) {
        //todo: get params from request
        $collection->getSelect()->limit(10);
    }
    return $this;
}

The event gets dispatched in app/code/core/Mage/Catalog/Block/Product/List.php:161.

10
  • I tried this code But there is no change in the output. it loads the total number of items in a collection. I tried an echo inside the function catalogBlockProductListCollection but that is not output to the browser. So i assume it is not calling this method. Any suggestions.
    – zamil
    Nov 14, 2013 at 9:57
  • My observer.php is in Model/Observer.php
    – zamil
    Nov 14, 2013 at 10:06
  • Your section models might be called different then ziva_customcatalog. Take care to clear the config cache after assuring your module is enabled via app/etc/modules and the observers is referenced correctly according to: <class>ziva_customcatalog/observer</class> Nov 14, 2013 at 10:30
  • this is the error i am getting Warning: include(Mage\Ziva\Customcatalog\Model\Observer.php)
    – zamil
    Nov 14, 2013 at 11:10
  • Please show how you reference your models within config.xmls global section? magentocommerce.com/wiki/5_-_modules_and_development/reference/… Nov 14, 2013 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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