Christoffer Bubach
Christoffer Bubach

Reputation: 1686

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...

Upvotes: 0

Views: 1534

Answers (1)

freento
freento

Reputation: 2949

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)

Upvotes: 1

Related Questions