Magento – Create magento pagination in custom product collection inside phtml file

collection;custom-optionsmagento-1.8pagination

First of all, i didn't create custom module to handle this product collection, so, i just put model inside phtml file, i know it's bad implementation, i'm new in magento and i have to do small customization in loading product collection to the extension that bought by client.

This product collection is only showing up in specific category, so inside custom design tab in category management, i put this code :

<reference name="product_list">
<action method="setTemplate"><template>catalog/product/list_all.phtml</template></action>

And here is my query to load product collection, this query is inside list_all.phtml file :

$collection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addStoreFilter()
            ->addUrlRewrite()
            ->addAttributeToFilter('groupdeal_status', Devinc_Groupdeals_Model_Source_Status::STATUS_RUNNING)
            ->joinField('groupdeals_id','groupdeals/groupdeals','groupdeals_id','product_id=entity_id',null,'left');
    $collection->getSelect()->limit(2);

i've used this code to generate the pagination :

    $toolbar = Mage::getBlockSingleton('catalog/product_list')->getToolbarBlock();
    $toolbar->setCollection($collection);
    echo $toolbar->toHtml();

the code above only showing the products count and show per-page options, not 1,2,3…, links, i've also tried to use the code below :

<?php 
$toolbar = Mage::getBlockSingleton('catalog/product_list')->getToolbarBlock();


$toolbar->setCollection($collection);
echo $toolbar->getCollection()->getSize().'------'.var_dump($toolbar->getPages());
?>

but, the var_dump returning NULL values. Should i create custom module to do this? or is there any ways to do this way, without creating custom module?? I've been trying to find a solutions about this, but most of the solution is using custom module.

Best Answer

Your concept is slightly lacking. The query should be at least moved outside of the template into Block class.

Then in the same class you can add something like this to solve your pagination issue:

protected function _beforeToHtml()
{
    $blockName = $this->getToolbarBlockName();
    if (!$blockName) {
        $blockName = 'product_list_toolbar';
        $this->setToolbarBlockName($blockName);
        $this->getLayout()->createBlock(
            $this->_defaultToolbarBlock,
            $blockName,
            array(
                 'show_toolbar' => $this->getData('show_toolbar'),
                 'list_mode' => $this->getData('list_mode'),
                 '_current_limit' => $this->getData('limit')
            ))
            ->setChild(
                $blockName . '_pager',
                $this->getLayout()->createBlock('page/html_pager', $blockName . '_pager')
            );
    }

    return parent::_beforeToHtml();
}

Unless you make fun of reinventing the wheel you can also use this extension to create a listing of all products of your store along with couple of other useful listings:

https://github.com/tim-bezhashvyly/CustomListing

Related Topic