Magento – Toolbar not displaying with overridden Mage_Catalog_Block_Product_List

collection;magento-1.7overridestoolbar

I am banging my head against the wall on the following problem: To get a product list that is sorted by the subcategory string, an product attribute and finally by the product name I have overridden the Mage_Catalog_Block_Product_List class to provide my on construction of the list.

I first search for all categories and sub-categories at the current category level. Then I iterate over the found categories and fetch the product sub-lists of each category. These sub-lists are concatenated to an results array with all products in the current category as well as subcategories. Having this results array I use the native php function array_slice() to get the results for the current page (retrieved with Mage::getBlockSingleton('page/html_pager')->getCurrentPage().

I then create an empty collection like

$merged = Mage::getModel('catalog/product')
    ->getCollection()
    ->addFieldToFilter('entity_id',-1);

$sliced_results = array_slice($this->_results, $start, $length);

and iterate over the array slice of products and use addItem to fill the collection:

foreach ($sliced_results as $result) {
    if (!$merged->getItemById($result->getId()))
        $merged->addItem($result);
}

I assign my collection to the _productCollection member and return it.

All this works fine so far, sorting is just like I want to have it, but the Paging toolbar in the frontend will not show up.

When I use

$merged = Mage::getResourceModel('catalog/product_collection');

I will get an collection of all store products (OK, that does not wonder me), in other words I see my added products in the frontend plus all the other products of the entire store. But in this case I see the Toolbar.

So my problem is I do not know how to get to show up the pager toolbar with my collection.

All above described functionality is implemented in the overridden method _getProductCollection so I normally expect it to work. In the _beforeToHtml method in the overriden Mage_Catalog_Block_Product_List class it seems my collection also 'arrives', but I have the strong feeling, some information is missed. I tried to add setCurPage and setPageSize but nothing helps.

I would be so happy, if I could solve this problem. I am relatively new to Magento, it is my first project with this framework, so please bear with me.

Any help is greatly appreciated!!

Best Answer

Without actually running your code, the reasons for this can be difficult to find.

This is not a solution for the actual issue, but a description on how to debug this type of issue. I hope it helps you towards solving the issue. I hope it also shows to others how important it is to use breakpoints with a debugger to solve a lot of magento 'why the f**ck is this not working' issues.

Are you using a debugger? This type of issue is easy to solve if you use a debugger, and step through the code, and check all the variables, values etc. Without this you are really just taking pot shots in the dark.

btw, I use netbeans with xdebug. (just in case you need a solution for that ;) )

Now, that all said, this is how I would start with the debug of this.

In list.phtml the toolbar is fetched via the code <?php echo $this->getToolbarHtml() ?>, thus I would start by whacking a breakpoint on that line, and then step into the routine to find why...

ok, so stepping in, I find this code:

/**
     * Retrieve list toolbar HTML
     *
     * @return string
     */
    public function getToolbarHtml()
    {
        return $this->getChildHtml('toolbar');
    }

thus, is the child block attached?

enter image description here

yes it is, so that is not the issue. Lets see what template is used for this block. Using the debugger, I can expand the toolbar child block, and view all its properties/values etc. The template is:

enter image description here

So, lets go do a break point in that file, and see code flow from there...

The first line of code in that template is <?php if($this->getCollection()->getSize()): ?>

Using the debugger, I can step over this line, and confirm it evaluates to true, if not ? why not....

And here my example ends, as the line in the tolbar could be your issue....

I hope this example helps, and show you how important it is to use a real debugger when coding with magento (or any application for that matter) :)

Using breakpoints, and stepping through the code, and actually have the ability to view all the classes/objects/values etc during a run, makes it pretty easy to solve this type of issue.

Have fun :)

Related Topic