1

I am attempting to set up an observer that monitors category view. Prior to HTML output I need to "hijack" this collection if it is an ajax request so that I do not have to rebuild the collection query manually (limit, page, filters, etc...).

What I am attempting is below. Is there a straightforward way of to fetch the current collection prior to HTML output?

class MyMod_Sliderview_Model_Observer {

public function catalogCategoryViewPredispatch($observer) {
  if ($this->getParam('ajax') || $this->getParam('isAjax')) {
    $helper = Mage::helper('core');
    $controller = $observer->getEvent()->getControllerAction();
    $request = $controller->getRequest();
    $params = $request->getParams();

    //Show me 
    zend_debug::dump(Mage::getModel('catalog/category')->getProductCollection());
    }
  }
}

Edit The reason I am not filtering is because I am hoping to inherit the product collection from the category view by using the observer. I want the same collection that is sent to list.phtml in the category product list template.

Edit 2: I managed to get the observer to output the collection data I was expecting (based on your guidance) but my problem is that the data isn't being generated until after HTML output has already begun. I've crawled through the other event listeners and from what I can gather the collection isn't built until after HTML is being generated. It looks like I have no choice but to try and imitate the collection request based on parameters (page, limit, filters, etc.)

Thanks, this has me feeling optimistic. I have updated the config to...

        <catalog_product_collection_load_after>
            <observers>
                <mymod_sliderview_model_observer>
                    <type>singleton</type>
                    <class>Mymod_Sliderview_Model_Observer</class>
                    <method>catalogProductCollection</method>
                </mymod_sliderview_model_observer>
            </observers>
        </catalog_product_collection_load_after> 

and my model to..

class Mymod_Sliderview_Model_Observer {
    public function catalogProductCollection($observer) {  
      $event = $observer->getEvent();
      $collection = $event->getCollection(); 
      zend_debug::dump($collection);
    }
}
  • 1
    you are getting all the products because you call Mage::getModel('catalog/category')->getProductCollection(). You don't filter the results by category and you don't limit the number of products anywhere – Marius Feb 9 '15 at 16:01
  • Marius, thank you for the response. The reason I am not filtering is because I am hoping to inherit the product collection from the category view by using the observer. Can I use this observer or piggy back elsewhere so that I can load the same collection that is sent to list.phtml in the category product list template? – TylersSN Feb 9 '15 at 16:03
  • If you use the predespatch event you have to recreate the collection yourserlf. The blocks are not instantiated yet and nothing is processed. you can try to use controller_action_layout_generate_blocks_after, but that's a general event. it will be called for all pages. You need to make sure you are on the right page, otherwise, do nothing – Marius Feb 9 '15 at 16:11
  • I'm looking into the controller_action_layout_generate_blocks_after but, still, maybe I should approach this by simply asking.. Is there a straightforward way for me to query for the product collection that Magento is rendering before any HTML output? – TylersSN Feb 9 '15 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.