3

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.

3

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

  • Hi Tim, thanks for your reply, and for your extension, i've decided to use your extension, instead of using CMS page, can i use it in custom design tab inside category management? i've been trying to do that, but still no luck. Thanks! – user3429615 Mar 18 '14 at 7:28
  • No, the listing should be inside of CMS page but you can use design tab there. – user487772 Mar 18 '14 at 7:39
  • Sorry for late reply Tim, thanks a lot!, it's working now. – user3429615 Mar 24 '14 at 12:28
3

This code help me with issues with pagination toolbar:

        $toolbar = Mage::getBlockSingleton('catalog/product_list')->getToolbarBlock();
        $toolbar->setCollection($_productCollection);
        $layout = Mage::getSingleton('core/layout');
        $pager = $layout->createBlock('page/html_pager');
        $toolbar->setChild('product_list_toolbar_pager', $pager);
        echo $toolbar->toHtml();  // to Show toolbar
  • Yup this is working.. awesome +1 – Amir Iqbal Sep 24 '16 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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