Magento – Check Some Condition Before Product added to cart

addtocartcartevent-observermagento-1.9

I want to add product to cart. So when i click on the add to cart button i will be checking some condition like some variable is set or not. If it is set, product will be added to cart. If not set, the product should not be added to cart.

for this i was using observer called controller_action_predispatch_checkout_cart_add

I can wrote a condition to check variable is set or not.if it is not set i want to come out of the observer. I mean product should not added to cart.

Please Help Me.

Thanks In Advance

Best Answer

The closest events I could find that would work that are lower than the dispatch event are

catalog_product_type_prepare_full_options
catalog_product_type_prepare_lite_options

You can have an event listener on one of those events that does your logic check and throws an exception if it fails.

That being said, Magento itself actually uses the following event

checkout_cart_product_add_after

Then, in the listener is uses code like this

$quoteItem = $observer->getEvent()->getQuoteItem();
$product = $observer->getEvent()->getProduct();
// Your checks here, return if you want to leave things alone
$quoteItem->getQuote()->removeItem($quoteItem->getId());
Mage::throwException('This is the message to the customer');
Related Topic