Search code examples
phpmagentoobservers

abort product add from sales_quote_add_item observer


In my sales_quote_add_item-observer how would I abort the product add with a custom error message in a standard way? Obviously tried to Google it and check the core source without getting much wiser...


Solution

  • You may throw new exception. Like:

    Mage::throwException(
                Mage::helper('sales')->__('Nominal item can be purchased standalone only. To proceed please remove other items from the quote.')
            );
    

    Have a look to the class and method, which dispaches sales_quote_add_item event:

    class Mage_Sales_Model_Quote extends Mage_Core_Model_Abstract
    ...
    public function addItem(Mage_Sales_Model_Quote_Item $item)
    

    It also throws the exception, which will be catched in controller after and will be displayed correctly.

    Also you may try this:

    $observer->getEvent()->getQuoteItem()->getQuote()->addErrorInfo(..);
    Mage::throwException(..);
    

    Here is the description of addErrorInfo function:

    public function addErrorInfo($type = 'error', $origin = null, $code = null, $message = null, $additionalData = null)