I want to display a notification or message to the user; when he adds an item to the cart, which is limited to '1' item max. a message like : 'You can add 1 item maximum' . I did an observer to block the add but it shows me the error message in the log default magento. it's not pretty,I want to display it in the same page

My observer:

// Event: catalog_product_type_prepare_full_options    

public function limit_(Varient_Event_Observer $observer) {

    $quote = Mage::getSingleton('checkout/session')->getQuote();
        Mage::throwException('You can add 1 item maximum.');


I'm assuming that throwing the exception prevents the actual item being added? If that's the case then you probably just want to do something like:

public function limit_(Varient_Event_Observer $observer) 
    $session = Mage::getSingleton('checkout/session');
    $quote = $session->getQuote();

        $message = 'You can add 1 item maximum.';

Assuming that your blocks on the frontend are all pretty vanilla, they should output the Mage messages on page load which will include this error message added to the session.

Just a note, that the logic you've got here will only allow a single item to be added to the cart. In its entirety.

If you sold Hats and Shirts, this would allow you to only purchase 1 hat or 1 shirt. Is this the functionality you want or do you want it to be more like 1 hat and 1 shirt or 1 hat and 0 shirt or 0 hat and 1 shirt?

If so, you will need to drill into the $quote->getItems() and validate that the item you're adding has not already been added to the quote.

| improve this answer | |
  • Your solution works like the one I had done before, that is to say the exception is displayed well but still in the error log magento. like this: "There has been an error processing your request You can add 1 item maximum. Error log record number: 855906023792" But I don't want to display in this area(the log page error). but in my current page style a javascript notification or something like this. – Miroo Oct 25 '15 at 15:37
  • Oh I see. Mage::throwException($message) automatically logs it. So just replace it with throw new Exception($message) – Luke Rodgers Oct 25 '15 at 15:39
  • Now is better, but there is a problem. it appears to me first in the error log magento. then when I go back to the page I find find the message as a notification. I dont know why it appears to me on the error page first! I dont need that ! – Miroo Oct 25 '15 at 15:54

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.