Organizational Research By

Surprising Reserch Topic

getting the first collection item without breaking pager

getting the first collection item without breaking pager  using -'magento'

I posted a question about this earlier, but I have more information now and I thought it best to post a new one rather than modify (sorry if that's not proper protocol).  You can find my original question here.

Anyhow, the initial problem was that I wanted to inspect the first item in a collection inside the List.php class just after setting the collection so that I could scrape the category and use that to show reviews.  This was all based off a custom module, so there were a lot of variables.  I have since tried it on a default Magento sample store and only added ONE line to app/code/core/Mage/catalog/Block/Product/List.php to break the pager.  Here are the details.  If you have any ideas why this is happening, please let me know as I'm stuck

First, open app/code/core/Mage/catalog/Block/Product/List.php and locate the _getProductCollection function.  At the end of the if (is_null...) block, add $_foo123 = $this->_productCollection->getFirstItem(); so you have a function that looks like this:

protected function _getProductCollection()
    if (is_null($this->_productCollection)) {
        $layer = $this->getLayer();
        /* @var $layer Mage_Catalog_Model_Layer */
        if ($this->getShowRootCategory()) {

        // if this is a product view page
        if (Mage::registry('product')) {
            // get collection of categories this product is associated with
            $categories = Mage::registry('product')->getCategoryCollection()
            ->setPage(1, 1)
            // if the product is associated with any category
            if ($categories->count()) {
                // show products from this category

        $origCategory = null;
        if ($this->getCategoryId()) {
            $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
            if ($category->getId()) {
                $origCategory = $layer->getCurrentCategory();
        $this->_productCollection = $layer->getProductCollection();


        if ($origCategory) {

        $_foo123 = $this->_productCollection->getFirstItem();

    return $this->_productCollection;

Now, simply go to any product list that uses that class (category view, for instance) and you'll see what I mean.  No matter what you choose under Show XX per page in the toolbar, it will always show you all items in the list.  If you comment out that $_foo123... line, it works fine.

What gives??

P.S.  I know I'm not supposed to edit core files...this is just an example :)

asked Oct 23, 2015 by sachin wagh
0 votes

Related Hot Questions

1 Answer

0 votes
answered Oct 23, 2015 by mcasudhir