Magento : Invalid argument supplied for foreach in toolbar.phtml on line 63

Friday, 18. November 2011

Invalid argument supplied for foreach in toolbar.phtml is a common error abserved mostly when upgrading from very old versions of Magento notably 1.3 and below to latest versions.

Here are steps to follow in order to solve the problem.

1. Create Template file for Pagination

Create a new file called pager.phtml in the /template/catalog/product/list/ folder inside your theme folder and add this code.

<?php if($this->getCollection()->getSize()): ?>
        <?php if($this->getLastPageNum()>1): ?>
        <td class="pages">
            <strong><?php echo $this->__('Page:') ?></strong>
            <ol>
            <?php if (!$this->isFirstPage()): ?>
                <li><a href="<?php echo $this->getPreviousPageUrl() ?>"><img src="<?php echo $this->getSkinUrl('images/pager_arrow_left.gif') ?>" alt="<?php echo $this->__('Previous Page'); ?>" /></a></li>
            <?php endif ?>
            <?php foreach ($this->getPages() as $_page): ?>
                <?php if ($this->isPageCurrent($_page)): ?>
                    <li><span class="on"><?php echo $_page ?></span></li>
                <?php else: ?>
                    <li><a href="<?php echo $this->getPageUrl($_page) ?>"><?php echo $_page ?></a></li>
                <?php endif ?>
            <?php endforeach;; ?>
            <?php if (!$this->isLastPage()): ?>
                <li><a href="<?php echo $this->getNextPageUrl() ?>"><img src="<?php echo $this->getSkinUrl('images/pager_arrow_right.gif') ?>" alt="<?php echo $this->__('Next Page'); ?>" /></a></li>
            <?php endif ?>
            </ol>
        </td>
        <?php endif; ?>
<?php endif ?>

2. Now open file catalog.xml

Locate and edit the file catalog.xml from your theme’s layout folder. Search for this line of code:

<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">

Add this line underneath.

<block type="page/html_pager" name="product_list_toolbar_pager" template="catalog/product/list/pager.phtml" />

2. Now open file toolbar.phtml

Finally locate the file toolbar.phtml from your theme/template/catalog/product/list/. Now search for the below block of code approximately around lines 42 – 63:

<?php if($this->getLastPageNum()>1 && is_array($this->getPages())): ?>
...
...
...
<?php endif; ?>

Now replace this block of code with the following code:

<?php echo $this->getPagerHtml(); ?>

Enjoy!

Leave a Reply

You must be logged in to post a comment.