0

In my cart observer, I block 1 item in the cart and then display a notification to the user something like "you could select just one item" the problem that it appears to be the error log Magento first then when I return to the home page. I find my message displayed. I just want to display it in my page and not also in the error log page or display it in the cart page without adding a second item of course.

Observer.php :

class Amir_Prince_Model_ObserverCountItemCart{
    public function limiter_panier(Varient_Event_Observer $observer) {

    $session = Mage::getSingleton('checkout/session');
    $quote = $session->getQuote();

    if($quote->getItemsCount()>=1){
    $message = 'You can select only one item.';
    $session->addError($message); 
    // Mage::thrownewException($message); // the log error commented
    throw new Exception($message);// the frontend notification

      }
    }
}
2

You'll need to remove throw new Exception($message); to stop getting log messages in the exception log. Its also better to use the methods:

Mage::getSingleton(‘core/session’)->addNotice(‘Notice message’);
Mage::getSingleton(‘core/session’)->addError(‘Error message’); 
Mage::getSingleton(‘core/session’)->addWarning(‘Warning message’); 
Mage::getSingleton(‘core/session’)->addSuccess(‘Success message’);

Depending on how you want the message to appear.

  • This method i'll put it where ? in my observer ! i tried to replace: throw new Exception($message); with Mage::getSingleton(‘core/session’)->addNotice(‘Notice message’); but it doesnt work – Miroo Nov 4 '15 at 10:40
  • Is this an ajax action? If you refresh the page does the message show up? – rob3000 Nov 4 '15 at 11:22
  • When i replaced throw new Exception($message); with Mage::getSingleton(‘core/session’)->addNotice(‘Notice message’); I got an Error. No, I want to display it only once each time the user want to add a 2nd item to the cart , I am willing to display it in cart page. here is my process in 4 phases: 1)Index.phtml->2)Result.phtml->3)Cart.phtml->4)Checkout.phtml in phase 2)Result.phtml i do a action button to add to the cart. – Miroo Nov 4 '15 at 13:14
  • what was the error? – rob3000 Nov 5 '15 at 0:08
  • I verified my code, i havnt error now but my observer dont work, i mean : my observer dont block the add. – Miroo Nov 5 '15 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.