2014-09-04 74 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調用。