0

Is there any observer event on frontend home page load?

On my custom module, I want to display a notice message on home page if certain conditions are met. Can I do this using event-observer?

4
  • why you dont try hadler and check your condition in your block cms_index_index home page handler Sep 14, 2016 at 11:25
  • Thanks! actually i want to display a block on the header portion of almost all the frontend pages. Is using handler the right choice? how do i implement this? Sep 14, 2016 at 11:51
  • You want to display on home page or all pages?? Sep 14, 2016 at 11:54
  • then my suggestion is to create widget not to go with other solution , becuse u can create widget from admin panel and it easily to maintain and good thing is that you dont have to write code for this Sep 14, 2016 at 12:01

2 Answers 2

2

Add the following in the extension's config.xml

        <events>
        <controller_action_layout_load_before>
            <observers>
                <abandonedcart_observer>
                    <type>singleton</type>
                    <class>abandonedcart/observer</class>
                    <method>showCouponCodeWhenLoad</method>
                </abandonedcart_observer>
            </observers>
        </controller_action_layout_load_before>
    </events>

And here is the code to go in showCouponCodeWhenLoad() function in Observer.php

    $routeName = Mage::app()->getRequest()->getRouteName();
    $identifier = Mage::getSingleton('cms/page')->getIdentifier();

    if($routeName == 'cms' && $identifier == 'home') {
         // do something
    }

Hope this helps to someone!

1

There is controller_action_predispatch_cms_index_index (before home page is loaded) and controller_action_postdispatch_cms_index_index (after home page is loaded). In the first event, the layout is not loaded yet and in the second, the complete home page is already rendered.

So for your requirement, these observers are not very useful. You could use controller_action_layout_load_before to conditionally add a block to the layout (with an additional check if the current page is the home page).

But a better solution is to not use observers at all, but add a block to the home page via layout XML and implement the conditions in its _toHtml() method: If nothing should be displayed, just return "". And if you want to have this block cached, make sure to use a different cache key dependent on the condition.

4
  • Thanks! i will try controller_action_layout_load_before for home page for now. But later I would want to display a block in most of the pages' header. How can i accomplish in custom module,thanks! Sep 14, 2016 at 12:05
  • This event is triggered on all pages, so you can decide in the observer, for which pages you add the block. Sep 14, 2016 at 12:10
  • I use if($routeName == 'cms' && $identifier == 'home') and its working fine. Thanks!! Sep 14, 2016 at 12:27
  • Great, please take the time to accept the answer if it helped you. Sep 14, 2016 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.