我的观察者似乎没有捕捉到 Magento v1.12.0.2 发出的事件。我正在密切关注来自http://inchoo.net/ecommerce/magento/dispatching-before-and-after-events-to-magento-core-actions/的教程,并且似乎无法重现。
app/etc/modules/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>
app/code/local/Hatclub/RequireAdditionalProduct/Model/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)。两者都没有输出任何东西,所以这似乎根本不起作用。谁能发现我哪里出错了?