正如我们所知的,Magento中有些事件被默认地调度。我们可以轻易地挂钩这些事件,但这不是这篇文章的主题。我们要尝试的是在一些Magento控制器的一些动作前后添加自定义事件。 这样,我们就不需要事件被默认调度。当然,有些事件需要多做一点处理(例如,如果你需要一些session的东西,session在动作之后要被清除),但是大部分情况,有一种方法可以从事件中获取我们想要的。 例如,“Mage_Checkout_CartController”中的“addAction()”方法,我们可以用标准的方式重写这个方法并在重写的类里调度我们的事件。但我们可以做些类似这样的事情。 添加下面的代码到你模块的config.xml文件: < ?xml version="1.0"?> <config> <global> <models> <dispatcher> <class>Alwayly_Dispatcher_Model</class> </dispatcher> </models> </global> <frontend> <events> <!-- Hooking to Magento's default event "controller_action_predispatch" --> <controller_action_predispatch> <observers> <controller_action_before> <class>dispatcher/observer</class> <method>hookToControllerActionPreDispatch</method> </controller_action_before> </observers> </controller_action_predispatch> <!-- Hooking to Magento's default event "controller_action_postdispatch" --> <controller_action_postdispatch> <observers> <controller_action_after> <class>dispatcher/observer</class> <method>hookToControllerActionPostDispatch</method> </controller_action_after> </observers> </controller_action_postdispatch> <!-- Hooking to our own event "add_to_cart_before" --> <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> <!-- Hooking to our own event "add_to_cart_after" --> <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> </frontend> </config> 添加下面的代码到你模块的Observer.php class Alwayly_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."); } } 最后,我要指出的是:这段代码没有在产品环境中做测试,我们只是添加了两个自定义事件在Magento核心动作的前后,而不必重写这个类。 (责任编辑:最模板) |