How to redirect from an Observer and send a message to the customer

The question:

I am using an Observer to catch that event: controller_action_postdispatch_checkout_onepage_saveBilling

I want that event in order to check the billing address of the customer.

My question is: How can i send a message to the user at this point.When continue button is pressed.

I used the : Mage::getSingleton('core/session')->addError('My message');

but the message is not shown until you refresh the page.And here is the second question.
How can i redirect the page from that point?
I have use with no luck: $this->_redirect('*/*/'); and hardcoded urls with no luck.
Ideally i want when the customer presses the continue button, after some checks that i make, the process to be stopped with a message.The observer is checked and it works as it supposed to work.

Hope it make sense..

That’s my code.Just to make sure i’m doing it right:

class Company_Restrictions_Model_Observer {

    public function notifyUser($observer) {
//Lot's off lines are tested here.Nothing seems to work..
//Edited after comments
$result = array();
$result['error'] = '-1';
$result['message'] = 'YOUR MESSAGE HERE';



<?xml version="1.0"?>


After debuuging i found out that a lot of functions are giving me error.Undefined method … for ex: _redirect() or Response() or Body().. Does anyone knows why??

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

The saveBilling result is only evaluated as part of an Ajax call. Take a look at the original implementation here:

public function saveBillingAction()
    if ($this->_expireAjax()) {
    if ($this->getRequest()->isPost()) {
        $data = $this->getRequest()->getPost('billing', array());
        $customerAddressId = $this->getRequest()->getPost('billing_address_id', false);

        if (isset($data['email'])) {
            $data['email'] = trim($data['email']);
        $result = $this->getOnepage()->saveBilling($data, $customerAddressId);

        if (!isset($result['error'])) {
            /* check quote for virtual */
            if ($this->getOnepage()->getQuote()->isVirtual()) {
                $result['goto_section'] = 'payment';
                $result['update_section'] = array(
                    'name' => 'payment-method',
                    'html' => $this->_getPaymentMethodsHtml()
            } elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1) {
                $result['goto_section'] = 'shipping_method';
                $result['update_section'] = array(
                    'name' => 'shipping-method',
                    'html' => $this->_getShippingMethodsHtml()

                $result['allow_sections'] = array('shipping');
                $result['duplicateBillingInfo'] = 'true';
            } else {
                $result['goto_section'] = 'shipping';


So if you want to send a different response / redirect you would also need to make use of a JSON encoded response

$controllerAction = $observer->getEvent()->getControllerAction();
$result = array();
$result['error'] = '-1';
$result['message'] = 'YOUR MESSAGE HERE';

One further thing to look into is if using the postDispatch provides the right timing.

Method 2

You can redirect from anywhere using

   ->setRedirect('[ your url ]')

you can add your checks in your observer and redirect back to the previous/current page if the checks fail or forward to some success page if the checks are passed

Method 3

From the Observer you can:-

$pageId = Mage::getStoreConfig('some/cms/redirect_to');


$pageId = Mage::getStoreConfig('some/cms/redirect_to');
      Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment