You must use $session->setErrorMessage(true);
instead of $session->hasErrorMessage(true);
, which is an isset()
shortcut (both are magic methods, you can check Varien_Object::__call()
to see what's their behaviour).
But then the message will get displayed only once per session, so you could also detect if the message was already added by using this code :
protected function _setErrorMessage()
{
$session = Mage::getSingleton('b2b/session'); /* extends Mage_Core_Model_Session */
$errorMessage = $this->helper->getErrorMessage();
$isMessageAdded = false;
foreach ($session->getMessages() as $message) {
if ($message->getText() == $errorMessage) {
$isMessageAdded = true;
break;
}
}
if (!$isMessageAdded) {
$session->addError($errorMessage);
}
return $this;
}