我的观察者似乎没有捕捉到Magento v1.12.0.2发出的事件。我正在密切关注http://inchoo.net/ecommerce/magento/dispatching-before-and-after-events-to-magento-core-actions/的教程,似乎无法重现。
应用的/ etc /模块/ Require_Additional_Product.xml :
<?xml version="1.0"?>
<config>
<modules>
<RequireAdditionalProduct>
<active>true</active>
<codePool>local</codePool>
</RequireAdditionalProduct>
</modules>
</config>
应用/代码/本地/ Hatclub / RequireAdditionalProduct的/ etc / config.xml中:
<modules>
<RequireAdditionalProduct>
<version>1.0.0</version>
</RequireAdditionalProduct>
</modules>
<global>
<models>
<dispatcher>
<class>Hatclub_RequireAdditionalProduct_Model</class>
</dispatcher>
</models>
<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>
</global>
应用/代码/本地/ Hatclub / RequireAdditionalProduct /型号/ Observer.xml :
class Hatclub_RequireAdditionalProduct_Model_Observer {
//this is hook to Magento's event dispatched before action is run
public function hookToControllerActionPreDispatch($observer)
{
error_log('test 1', 0);
//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)
{
error_log('test 1', 0);
//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)
{
error_log('test 1', 0);
//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)
{
error_log('test 1', 0);
// Hooking to our own event
$request = $observer->getEvent()->getRequest()->getParams();
// do something with product
Mage::log("Product ".$request['product']." is added to cart.");
}
}
我只是想获取输出,以便我知道观察者正在捕获事件(通过使用php error_log和Mage :: log)。两者都没有输出任何东西,所以看起来这根本不起作用。谁能找到我出错的地方?
答案 0 :(得分:4)
模块注册文件中的xpath对于模块配置文件不正确。
<?xml version="1.0"?>
<config>
<modules>
<!--
this node name along with codePool value is how your module's
config.xml file will be located.
-->
<Hatclub_RequireAdditionalProduct>
<active>true</active>
<codePool>local</codePool>
</Hatclub_RequireAdditionalProduct>
</modules>
</config>
有关详情,请参阅Mage_Core_Model_Config::loadModulesConfiguration()
(link)。