php - Set Error Message Once Per Page Request -
i'm trying create minimum total checkout module prevent checking out total less configurable amount.
i'm using event sales_quote_save_before
display error on checkout/cart page when it's opened.
<?xml version="1.0"?> <config> <frontend> <events> <sales_quote_save_before> <observers> <b2b> <class>b2b/observer</class> <method>checktotalscart</method> </b2b> </observers> </sales_quote_save_before> </events> </frontend> </config>
and in observer
public function checktotalscart() { if ($this->_hascarterror()) { /* checks, returns bool */ $this->_seterrormessage(); } } protected function _seterrormessage() { $session = mage::getsingleton("b2b/session"); /* extends mage_core_model_session */ $session->adderror($this->helper->geterrormessage()); }
the problem when update cart cart page, error message showing twice. guess event happening multiple times.
i've tried check if message set custom session variable
protected function _seterrormessage() { $session = mage::getsingleton("b2b/session"); if ($session->haserrormessage()) { return; } $session->adderror($this->helper->geterrormessage()); $session->haserrormessage(true); }
but didn't work either. how can make sure error message showing once per page request?
you must use $session->seterrormessage(true);
instead of $session->haserrormessage(true);
, isset()
shortcut (both magic methods, can check varien_object::__call()
see what's behaviour).
but message displayed once per session, detect if message added using code :
protected function _seterrormessage() { $session = mage::getsingleton('b2b/session'); /* extends mage_core_model_session */ $errormessage = $this->helper->geterrormessage(); $ismessageadded = false; foreach ($session->getmessages() $message) { if ($message->gettext() == $errormessage) { $ismessageadded = true; break; } } if (!$ismessageadded) { $session->adderror($errormessage); } return $this; }
Comments
Post a Comment