tags:

views:

514

answers:

1

Hi,

Im trying to limit the number of returned results manually in a copy of the list.phtml template, but its turning out to be alot harder than I anticipated.

Ive tried manually setting the collection size, but ya once again nothing working. Can someone show me how to do this? Would be nmuch appreciated!

A: 

It looks like the collection returned in list.phtml has already had load() called, which means that by the time we get to the template we've lost the opportunity to set the page size. So, this is going to get a bit messy!

The block that generates that collection is Mage_Catalog_Block_Product_List, which we can extend with our own class and override at the same time. Create a new block that extends Mage_Catalog_Block_Product_List and override the method _getProductCollection as follows:

/**
 * Retrieve loaded category collection
 *
 * @return Mage_Eav_Model_Entity_Collection_Abstract
 */
protected function _getProductCollection()
{
    if (is_null($this->_productCollection)) {
        $layer = Mage::getSingleton('catalog/layer');
        /* @var $layer Mage_Catalog_Model_Layer */
        if ($this->getShowRootCategory()) {
            $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
        }

        // 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)
                ->load();
            // if the product is associated with any category
            if ($categories->count()) {
                // show products from this category
                $this->setCategoryId(current($categories->getIterator()));
            }
        }

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

        $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

        // OUR CODE MODIFICATION ////////////////////
        $yourCustomPage = someFunctionThatDetectsYourCustomPage();
        if($yourCustomPage) {
            $this->_productCollection->setPageSize(1);
            $this->_productCollection->setCurPage(3);
            $this->_productCollection->load();
        }
        /////////////////////////////////////////////

        if ($origCategory) {
            $layer->setCurrentCategory($origCategory);
        }
    }
    return $this->_productCollection;
}

The important part is to find some way to detect whether you're using the custom list.phtml page or not. Then you'll need to override references to <block type='catalog/product_list' /> in the layouts with your class, and you should be set to go.

Hope that helps!

Thanks, Joe

Joseph Mastey
Hey Joe,Thanks for the reply. Ok so, can I put this in the custom list.php file (which Im using to make a landing page), before the other code or do I need to put it somewhere else? Im basically just extending this module;http://yoast.com/landing-pages-module-magento/-- to allow the limiting of the number of items returned, so I can make nice looking landing pages. Im a little confused about the part someFunctionThatDetectsYourCustomPage(); -- Do I call that function first on the landing.php page before everything else or? (sorry Im still fairly new to OO PHP and Zend and what not.)
thrice801