简体   繁体   English

Magento 1.6.2事件观察器不起作用

[英]Magento 1.6.2 event observer doesnt work

I am trying to implement observer in my module (app/code/community/Test/Shipping). 我正在尝试在我的模块(应用程序/代码/社区/测试/运输)中实现观察者。

My files are: app/code/community/Test/Shipping/etc/config.xml global part only 我的文件是:仅app / code / community / Test / Shipping / etc / config.xml全局部分

<global>
    <models>
        <test_shipping>
            <class>Test_Shipping_Model</class>
        </test_shipping>
    </models>
    <events>
        <checkout_type_onepage_save_order_after>
            <observers>
                <Test_Shipping_Observer>
                    <type>singleton</type>
                    <class>test_shipping/observer</class>
                    <method>checkout_type_onepage_save_order_after</method>
                </Test_Shipping_Observer>
            </observers>
        </checkout_type_onepage_save_order_after>
    </events>
</global>

app/code/community/Test/Shipping/Model/Observer.php i replaced some values in curl but its tested and works with correct values. app / code / community / Test / Shipping / Model / Observer.php我替换了curl中的一些值,但对其进行了测试,并使用正确的值。

<?php

class Test_Shipping_Model_Observer { 类Test_Shipping_Model_Observer {

public function checkout_type_onepage_save_order_after(Varien_Event_Observer $observer) {
    $requests = array(
        "username" => "test",
        "password" => "test",
        "environment" => "development",
        "action" => "ship",
        "service_id" => "1"
    );
    $url = "";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requests));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
}

} }

app/etc/modules/Test_Shipping.xml with starting and ending config tags to, couldnt get them in my code example. app / etc / modules / Test_Shipping.xml,带有开始和结束配置标签,无法在我的代码示例中获取它们。

<modules>
    <Test_Shipping>
        <active>true</active>
        <codePool>community</codePool>
    </Test_Shipping>
</modules>

But observer doens't work, can anyone help me please? 但是观察者不工作,有人可以帮我吗? Is there a way to check if method is called so i would know that observer works but my curl doesnt. 有没有一种方法可以检查方法是否被调用,所以我会知道观察者有效,但是我的卷曲却没有。

Can you show us the <config>/<modules> part of your config.xml ? 您能否向我们展示config.xml的<config>/<modules>部分?

Anyways, apart from the function name of your observer that is non-magento friendly, it should work (you should call like executeCurlAfterOrderSave() ) 无论如何,除了非magento友好的观察者的函数名之外,它都应该起作用(您应该像executeCurlAfterOrderSave()那样调用)

To be sure it is called : 确保它被称为:

  • Activate the Log functionnality in Backoffice (Menu System / Configuration / Developer / Log Settings 在Backoffice中激活日志功能(菜单系统/配置/开发人员/日志设置)

  • add a Mage::log('event just dispatched'); 添加一个Mage::log('event just dispatched'); just after the 之后

    Mage::dispatchEvent('checkout_type_onepage_save_order_after', array('order'=>$order, 'quote'=>$this->getQuote())); 法师:: dispatchEvent('checkout_type_onepage_save_order_after',array('order'=> $ order,'quote'=> $ this-> getQuote()));

that is called in the Mage_Checkout_Model_Type_Onepage (or Mage_Checkout_Model_Cart_Api is you're using the API) note: revert this change that's only for debugging purpose as you must not modify core files. Mage_Checkout_Model_Type_Onepage (或您正在使用API​​的Mage_Checkout_Model_Cart_Api )中调用的注释):还原此更改仅用于调试目的,因为您不能修改核心文件。

  • add a Mage::log('even should be captured here'); 添加一个Mage::log('even should be captured here'); in the beginning of your Observer method 在您的Observer方法的开头

  • test your code and look at the var/log folder to see if what happened (what has been logged) 测试您的代码并查看var / log文件夹以查看是否发生了什么(已记录的内容)

Good luck 祝好运

Your config.xml is wrong, 您的config.xml错误,

<global>
<models>
    <test_shipping>
        <class>Test_Shipping_Model</class>
    </test_shipping>
</models>   
</global>

 <events>
    <checkout_type_onepage_save_order_after>
        <observers>
            <Test_Shipping_Observer>
                <type>singleton</type>
                <class>test_shipping/observer</class>
                <method>checkout_type_onepage_save_order_after</method>
            </Test_Shipping_Observer>
        </observers>
    </checkout_type_onepage_save_order_after>
</events>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM