【问题标题】:Magento's Observer event isn't working and using webservices inside a Observer classMagento 的 Observer 事件不起作用,并且在 Observer 类中使用 Web 服务
【发布时间】:2014-09-04 15:14:22
【问题描述】:

我正在尝试使用来自网络服务的值创建动态产品折扣。

我在网上搜索了一些关于这个问题的指南,我发现我需要使用checkout_cart_product_add_aftercheckout_cart_update_items_after

但是,我遵循了一些指南。创建了我自己的模块(在 Magento 后台可见:配置 > 高级 > 模块)和该模块的观察者。我没有再创造任何东西,但它不起作用。由于我可以在该菜单中看到模块,我相信问题出在观察者/事件调用上。

这是我的模块的 config.xml(位于 app\code\local\namespace\MyModule\etc 内):

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <namespace_MyModule>
            <version>0.1.0</version>
        </namespace_MyModule>
    </modules>
    <global>
        <events>
             <checkout_cart_product_add_after>
                   <observers>
                        <namespace_MyModule_Discount>
                           <class>MyModule/Observer</class>
                           <method>MyModulePriceChange</method>
                         </namespace_MyModule_Discount>
                    </observers>
              </checkout_cart_product_add_after>
         </events>
     </global>
</config>

这是我的模块的 Observer(位于 app\code\local\namespace\MyModule\Model 内):

<?php
    class namespace_MyModule_Model_Observer
    {
        public function MyModulePriceChange(Varien_Event_Observer $obs)
        {
            // Get the quote item
            $item = $obs->getQuoteItem();
            // Ensure we have the parent item, if it has one
            $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
            // Load the custom price
            $price = $this->_getPriceByItem($item);
            // Set the custom price
            $item->setCustomPrice($price);
            $item->setOriginalCustomPrice($price);
            // Enable super mode on the product.
            $item->getProduct()->setIsSuperMode(true);
        }

        protected function _getPriceByItem(Mage_Sales_Model_Quote_Item $item)
        {
            $price = 4;

            //use $item to determine your custom price.

            return $price;
        }

    }
?>

另外,是否可以调用soap客户端来使用观察者内部的网络服务?

希望我的问题很清楚,提前感谢您的帮助。

【问题讨论】:

    标签: web-services magento soap module observers


    【解决方案1】:

    我发现您的 config.xml 存在一些问题。首先,使用大写的公司名称和模块名称。 namespace_MyModule 将成为您的命名空间。 您必须像这样在全局部分下声明模型:

    <models>
        <mycompany_mymodule>
            <class>Mycompany_Mymodule_Model</class>
        </mycompany_mymodule>
    </models>
    

    这将告诉magento你想在你的模块中使用mycompany_mymodule,每个模块的类名将以Mycompany_Mymodule_Model开头。 Mycompany 和 Mymodule 分别对应于您的模块的文件夹:app/code/local/Mycompany/Mymodule。

    config.xml 的 modules 部分也应该有这个命名空间 (Mycompany_Mymodule),匹配文件 app/etc/modules 的名称和 app/code/local 中的文件夹结构。

    然后观察者变成以下(我添加了类型,并更改了类):

    <observers>
        <namespace_MyModule_Discount>
            <type>singleton</type>
            <class>mycompany_mymodule/Observer</class>
            <method>MyModulePriceChange</method>
        </namespace_MyModule_Discount>
    </observers>
    

    然后尝试通过添加一些类似die("message") 的代码来测试您的观察者方法。

    【讨论】:

      【解决方案2】:

      您尚未在 config.xml 文件中声明模型标签。 观察者毕竟是一个模型,magento 不知道在哪里找到它(您引用的 MyModule/Observer )。下面是一个声明models标签的例子:

      <models>
         <MyModule>
            <class>Namespace_Modulename_Model</class> 
         </MyModule>
      </models>
      

      是的,你可以在观察者内部调用soap api。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-02
        • 2018-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多