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..
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??
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()) {
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.
Method 2
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
Method 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 );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0