2014-09-04 71 views
0

我试图使用webservice中的值创建动态产品折扣。Magento的观察者事件不起作用,并在Observer类中使用webservices

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

但是,我遵循了一些指南。创建了我自己的模块(在Magento后台可以看到:配置>高级>模块)以及该模块的观察者。我没有创造更多,但它不起作用。由于我可以在该菜单中看到该模块,因此我认为问题出在观察者/事件调用上。

以下是config.xml文件(这是内部应用程序\代码\本地\命名空间\ MyModule的\等)为我的模块:

<?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> 

这是我的观察(这是内部应用程序\代码\当地\命名空间\ MyModule的\模型)对我的模块:

<?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服务?

我希望我的问题很清楚,先谢谢您的帮助。

回答

0

我发现你的config.xml存在一些问题。首先,使用大写的公司名称和模块名称。 namespace_MyModule将成为你的名字空间。 你必须根据全球部分申报模式是这样的:

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

这将告诉您要使用mycompany_mymodule为模型的模块在Magento,并且每个模块的类名称将与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")一些代码来测试你的观察方法。

0

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

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

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