As we all probably know, there are some events in Magento that are dispatched by default.
We can easily hook on those events, but this is not a subject of this article.
What we are going to try now is to add custom event before and after some action in some of Magento’s controllers.
That way we don’t need to have event dispatched by default.
Of course, there are some actions that require little more things to handle (for example if you need something from session and session is cleared after action is run), but
for most of the situations there is a way to get what we need from that event.
For example, let’s take “addAction()” from “Mage_Checkout_CartController”.
We could rewrite that action using standard way and dispatch our own event in our rewriten class, but we can something like this:
Add this to your module config.xml
< ?xml version="1.0"?>
Inchoo_Dispatcher_Model
<!-- Hooking to Magento's default event "controller_action_predispatch" -->
dispatcher/observer
hookToControllerActionPreDispatch
<!-- Hooking to Magento's default event "controller_action_postdispatch" -->
dispatcher/observer
hookToControllerActionPostDispatch
<!-- Hooking to our own event "add_to_cart_before" -->
dispatcher/observer
hookToAddToCartBefore
<!-- Hooking to our own event "add_to_cart_after" -->
dispatcher/observer
hookToAddToCartAfter
Add this code to your module Observer.php
class Inchoo_Dispatcher_Model_Observer
{
//this is hook to Magento's event dispatched before action is run
public function hookToControllerActionPreDispatch($observer)
{
//we compare action name to see if that's action for which we want to add our own event
if($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add')
{
//We are dispatching our own event before action ADD is run and sending parameters we need
Mage::dispatchEvent("add_to_cart_before", array('request' => $observer->getControllerAction()->getRequest()));
}
}
public function hookToControllerActionPostDispatch($observer)
{
//we compare action name to see if that's action for which we want to add our own event
if($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add')
{
//We are dispatching our own event before action ADD is run and sending parameters we need
Mage::dispatchEvent("add_to_cart_after", array('request' => $observer->getControllerAction()->getRequest()));
}
}
public function hookToAddToCartBefore($observer)
{
//Hooking to our own event
$request = $observer->getEvent()->getRequest()->getParams();
// do something with product
Mage::log("Product ".$request['product']." will be added to cart.");
}
public function hookToAddToCartAfter($observer)
{
//Hooking to our own event
$request = $observer->getEvent()->getRequest()->getParams();
// do something with product
Mage::log("Product ".$request['product']." is added to cart.");
}
}
Of course, adjust naming to suit your needs.
For the end, I just want to point out that this code is not tested in production enviroment,
it is merely result of playing with events a bit.
What we have done now is that we have added 2 of our custom events before and after randomly chosen Magento core action without need to rewrite it’s class.
I’m sure that lots of people were playing with something similar, so if you feel like it, comment it. Maybe we can do something to improve it.