通过观察员捕捉Magento事件

Nic*_*ons 0 magento

我的观察者似乎未捕获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>
Run Code Online (Sandbox Code Playgroud)

app / code / local / 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>
Run Code Online (Sandbox Code Playgroud)

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.");
    }

}
Run Code Online (Sandbox Code Playgroud)

我只是想获取输出,以便知道观察者正在捕获事件(通过使用php error_log和Mage :: log)。都不输出任何东西,因此看来这根本不起作用。谁能发现我要去哪里错了?

ben*_*rks 5

模块注册文件的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>
Run Code Online (Sandbox Code Playgroud)

请参阅Mage_Core_Model_Config::loadModulesConfiguration()(链接)以获取更多信息。