0

I am creating an observer to cancel the addition to the cart with add_to_cart_before.

Here is my config.xml

Module_Stockespagne_Model

     <controller_action_predispatch>
        <observers>
           <controller_action_before>
              <class>dispatcher/observer</class>
              <method>hookToControllerActionPreDispatch</method>
           </controller_action_before>
        </observers>
     </controller_action_predispatch>

     <controller_action_postdispatch>
        <observers>
           <controller_action_after>
              <class>dispatcher/observer</class>
              <method>hookToControllerActionPostDispatch</method>
           </controller_action_after>
        </observers>
     </controller_action_postdispatch>

     <add_to_cart_before>
        <observers>
           <add_to_cart_before>
              <class>dispatcher/observer</class>
              <method>hookToAddToCartBefore</method>
           </add_to_cart_before>
        </observers>
     </add_to_cart_before>

     <add_to_cart_after>
        <observers>
           <add_to_cart_after>
              <class>dispatcher/observer</class>
              <method>hookToAddToCartAfter</method>
           </add_to_cart_after>
        </observers>
     </add_to_cart_after>

  </events>

Here is my Observer.php

class Module_Stockespagne_Model_Observer {

public function hookToControllerActionPreDispatch($observer)
{
    if($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add') 
    {
        Mage::dispatchEvent("add_to_cart_before", array('request' => $observer->getControllerAction()->getRequest()));
    }
}

public function hookToControllerActionPostDispatch($observer)
{
    if($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add') 
    {
        Mage::dispatchEvent("add_to_cart_after", array('request' => $observer->getControllerAction()->getRequest()));
    }
}

public function hookToAddToCartBefore($observer) 
{   
    $request = $observer->getEvent()->getRequest()->getParams();
    $product = Mage::getModel('catalog/product')->load($request['product']);

    if($product->getSku != "TEST"){
        //Display message erroer
    }
}

}

I check some conditions and if they are not validated I want to cancel the addition to the cart and I display an error message.

Thank you!

0

Is there a chance throwing an exception would halt execution?

public function hookToAddToCartBefore($observer) 
{   
    $request = $observer->getEvent()->getRequest()->getParams();
    $product = Mage::getModel('catalog/product')->load($request['product']);

    if($product->getSku != "TEST"){
        //Display message error
        Mage::throwException('Add error message here');
    }
}

If that doesn't work the only other thing I could think to do is set a registry flag in hookToAddToCartBefore() when the error condition presents itself. You could then check for that flag in hookToAddToCartAfter(), remove the newly added item, clear the message queue and replace with your new message. However, this feels pretty clumsy.

Hopefully the first approach works instead.

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.