0

私のオブザーバーは、Magentov1.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>

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>

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

}

オブザーバーがイベントをキャッチしていることを確認するために、単に出力を取得しようとしています(phperror_logとMage:: logを使用)。どちらも何も出力していないので、まったく動作していないようです。誰かが私が間違っているところを見つけることができますか?

4

1 に答える 1

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>

詳細についてはMage_Core_Model_Config::loadModulesConfiguration()、(リンク)を参照してください。

于 2013-01-06T22:03:21.223 に答える