magento - How to create page with pagination in magento 1.7

magento - How to create page with pagination in magento 1.7

September 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento - How to create page with pagination in magento 1.7

Magento Pagination: Displaying Most Viewed Products

Hello developers! Today we’re going to tackle a common issue - how to create a custom page with pagination for most viewed products in Magento 1.7.

Understanding the Current Setup

You’ve already made some great progress by creating a custom block Mage_Catalog_Block_Product_Mostviewed.php and linking it to your product collection using the reports module:

class Mage_Catalog_Block_Product_Mostviewed extends Mage_Catalog_Block_Product_Abstract
{
   public function __construct(){
        parent::__construct();
        $storeId = Mage::app()->getStore()->getId();
        $collection = Mage::getResourceModel('reports/product_collection')
        ->addViewsCount();
        $collection->getSelect()
        ->joinInner(array('e2' => 'catalog_product_flat_'.$storeId), 'e2.entity_id = e.entity_id');
        $this->setProductCollection($collection);
        return parent::_beforeToHtml();
    }
}

This code retrieves the most viewed products from your database and prepares them for display in your template. However, you’re encountering an issue when trying to display the pagination links.

The Pagination Problem

In your mostviewedlist.phtml file, you’ve added this line to include the toolbar (pagination):

<?php echo $this->getToolbarHtml() ?>

Despite this, the pagination isn’t appearing on the front-end. This usually happens due to a mismatch between the block and its template.

Resolving the Pagination Issue

The problem lies in how Magento handles templates for custom blocks. By default, it looks for templates in the following directory:

app/design/frontend/{package}/{theme}/template/catalog/product/mostviewedlist.phtml

However, since you’re using a flat table (catalog_product_flat_$storeId), you need to ensure your template is correctly linked to this block. Here are the steps:

  1. Create the correct template file: Make sure you have created the mostviewedlist.phtml file in the following location:
app/design/frontend/{package}/{theme}/template/catalog/product/mostviewedlist.phtml
  1. Check the template path: Ensure that the $this->getTemplate() method is returning the correct template path for your custom block.

  2. Clear cache and reindex: After making changes, always clear the Magento cache and run the reindex command to ensure data consistency:

php bin/magento cache:clean
php bin/magento indexer:reindex

Displaying Most Viewed Products with Pagination

With this setup, your most viewed products should now display correctly with pagination links. The template file will look something like this:

<?php if(!{$this->getProducts()}): ?>
    <p class="note-msg"><?php echo $this->__('We can\'t find any most viewed products.') ?></p>
<?php else: ?>
    <?php $_helper = $this->helper('catalogoutput'); ?>
    <?php $limit = 10; // Number of products to show per page ?>
    <div class="product-list">
        <?php foreach ($this->getProductList() as $_item): ?>
            <!-- Display product information here -->
        <?php endforeach; ?>
    </div>
    <?php echo $this->getToolbarHtml() ?>  <!-- This will display the pagination links -->
<?php endif; ?>

Remember to replace the comment with your own logic for displaying each product.

Conclusion

In this guide, we’ve learned how to create a custom page for most viewed products in Magento 1.7. By correctly setting up the block and linking it to the appropriate template, you can display these products with pagination links.

If you found this tutorial helpful, feel free to explore more of our Magento tutorials for advanced tips and tricks to boost your e-commerce development skills. Happy coding!

Magento 1 Catalog Database Extensions

Our network