Magento 1.7 Observer – How to Redirect from an Observer and Send a Message to the Customer

magento-1magento-1.7

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

EDIT
That's my code.Just to make sure i'm doing it right:
Model/Observer

class Company_Restrictions_Model_Observer {

    public function notifyUser($observer) {
//Lot's off lines are tested here.Nothing seems to work..
//$observer->getRequest()->setParam('return_url','http://www.google.com/');
//Edited after comments
$result = array();
$result['error'] = '-1';
$result['message'] = 'YOUR MESSAGE HERE';
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));    

}
}

config.xml

<?xml version="1.0"?>

<config>
  <modules>
      <Company_Restrictions>
          <version>0.1.0</version>
      </Company_Restrictions>
  </modules>
  <global>
    <models>
        <company_restrictions>
             <class>Company_Restrictions_Model</class>
        </company_restrictions>
    </models>
    <helpers>
        <company_restrictions>
             <class>Company_Restrictions_Helper</class>
        </company_restrictions>
    </helpers>
    <events>
      <controller_action_postdispatch_checkout_onepage_saveBilling>
        <observers>
          <notify_user>
            <type>singleton</type>
            <class>Company_Restrictions_Model_Observer</class>
            <method>notifyUser</method>
          </notify_user>
        </observers>
      </controller_action_postdispatch_checkout_onepage_saveBilling>     
    </events>
  </global>
</config>

EDIT AGAIN
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??

Best Answer

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()) {
        return;
    }
    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';
            }
        }

        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
    }
}

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';
$controllerAction->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

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

Related Topic