2

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!!

  • what about your template (.phtml), you didn't mention that. – Shathish Oct 4 '13 at 6:23
  • @Shathish I use a copy of list.phtml, have not changed anything in there, it just shows list view (no grid). Btw, the layered navigation / Filter are working fine and show the right content. – Andreas W. Wylach Oct 4 '13 at 6:30
1

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 :)

  • Sorry for the late reply! Thanks for your answer. Yes, I use xdebug because I also write native PHP code / classes besides Magento, so I am aware of this. On saturday I found the actual problem why the toolbar is not showing up. I will provide an answer for that. +1 one for your suggestion! – Andreas W. Wylach Oct 7 '13 at 9:17
1

Further investigation to my described problem I found out, that the method getSize returns 0 on my collection that was built up with addItem. The count method show the right amount of entries, because the collection is loaded. But Toolbar and the pager.phtml use the getSize method, thus with my collection, it will never show up.

A more detailed description on count and getSize can be found Difference between getSize() and count() on collection. Reading the first, accepted answer made the problem clear to me.

So I ended up writing my own pager for my special list. I do not need the sorting options pulldown in the toolbar, since I provide a specialized sorting already. The amount of results to show per page is also not very much of importance. So for the first version that solution is working fine so far.

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.