6

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

3 Answers 3

3

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.

2
  • I edit my question with your suggestion.But still it doesn't work..About the event, i searched it and they say that this is the correct event to catch if you want to check the billing address.
    – vbak
    Commented Nov 27, 2013 at 9:30
  • okay modified the code slightly to show you how to get the request from the event you are currently observing: Mage::dispatchEvent('controller_action_postdispatch_'.$this->getFullActionName(), array('controller_action'=>$this)); Commented Nov 28, 2013 at 1:52
7

You can redirect from anywhere using

Mage::app()->getResponse()
   ->setRedirect('[ your url ]')
   ->sendResponse();

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

6
  • I want to redirect to the same page with an error message.How can i do that?Because i tried your way and it just doing nothing.
    – vbak
    Commented Nov 26, 2013 at 19:02
  • Could you add an exit; after that? As far as I know that code should work Commented Nov 26, 2013 at 19:04
  • Yeah me too..That's why i'm asking.Do you think that has anything to do with the event that i catch?
    – vbak
    Commented Nov 26, 2013 at 19:07
  • Mage::app()->getResponse() ->setRedirect('checkout/cart') ->sendResponse(); I am using that but it does nothing..
    – vbak
    Commented Nov 26, 2013 at 19:14
  • stating the obvious but is your observer actually called? what happens if you put a die() there? Commented Nov 26, 2013 at 19:50
3

From the Observer you can:-

$pageId = Mage::getStoreConfig('some/cms/redirect_to');
Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl().$pageId)->sendResponse();
exit;

Or

$pageId = Mage::getStoreConfig('some/cms/redirect_to');
Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl().$pageId)->sendResponse();
$observer->getControllerAction()->setFlag('',
      Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true
);

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.