0

I've attempted to create a template with the block type "catalog/product_list_toolbar" so that I can get the number of products currently in the category collection, in the same way as in toolbar.phtml. However, the collection is null when I inspect it in my custom template. If I inspect the contents of $this in my template, I have the following:

array(2) {
["module_name"]=>
 string(12) "Mage_Catalog"
["type"]=>
 string(28) "catalog/product_list_toolbar"

In my local.xml, my layout instruction is simply as follows.

<reference name="catalog.leftnav">
        <block type="catalog/product_list_toolbar" name="filter_trigger"
               template="catalog/product/list/filter.phtml" />
    </reference>

Am I missing something obvious here? All I want to be able to do as output:

$this->__('%s product(s) found', $this->getTotalNum())
1

First Approach: Set collection via observer

You have to set the collection on your block. This can be done by listening to the event catalog_block_product_list_collection triggered just before rendering.

config.xml

Observer definition listening to event catalog_block_product_list_collection

<config>
    <frontend>
        <events>
            <catalog_block_product_list_collection>
                <observers>
                    <namespace_catalog_block_product_list_collection>
                        <type>singleton</type>
                        <class>namespace_module/observer</class>
                        <method>setProductCollection</method>
                    </namespace_catalog_block_product_list_collection>
                </observers>
            </catalog_block_product_list_collection>

Observer.php

public function setProductCollection (Varien_Event_Observer $observer)
{
    $blockName = 'filter_trigger';

    if ( $block = Mage::app()->getLayout()->getBlock($blockName) )
    {
        $block->setCollection ( $observer->getCollection() );
    }
}

Second Approach: Use method of original toolbar block

If the vanilla toolbar block (named product_list_toolbar) still exists in your layout and isn't tampered with, you can call its method from your template instead of fixing your block:

$totalNum = Mage::app()->getLayout()->getBlock('product_list_toolbar')->getTotalNum();

Third Approach: Rename toolbar block

If you already removed or simply don't need the vanilla toolbar block, you can override the toolbar block assignment by adding something like this to your theme's layout/local.xml:

<default><!-- use whatever layout handler is appropriate -->
    <reference name="product_list">
        <action method="setToolbarBlockName"><name>filter_trigger</name></action>
    </reference>
</default>

NB: Don't forget to disable or flush caches.

  • Thanks @pong - that makes sense. Only thing is that despite making sure the collection is being set in the observer on my block, $this->getCollection() is still null in the template, weirdly. I'm still using catalog/product_list_toolbar as the block type. Been stepping through it but can't see the issue - any possible ideas? – bsod99 Dec 4 '16 at 18:23
  • Added two possible solutions you might wanna give a shot. – pong Dec 7 '16 at 8:28
  • Cheers @pong - I think there's something custom in the site I'm working on which is preventing this from working. Inspecting existing observers doesn't reveal the issue though, so I've just changed the block type to catalog/product_list where I can access the collection and check getSize(), which is all I needed. Marking as solved. – bsod99 Dec 9 '16 at 0:48

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.