2012-03-15 160 views

回答

39

做的方式是添加一個觀察者,看起來此事件'sales_quote_add_item'

<events> 
    <sales_quote_add_item> 
     <observers> 
      <priceupdate_observer> 
       <type>singleton</type> 
       <class>mymodule/observer</class> 
       <method>updatePrice</method> 
      </priceupdate_observer> 
     </observers> 
    </sales_quote_add_item> 
</events> 

觀察員應該有哪些不一樣的東西的方法這樣的:

public function updatePrice($observer) { 
    $event = $observer->getEvent(); 
    $quote_item = $event->getQuoteItem(); 
    $new_price = <insert logic> 
    $quote_item->setOriginalCustomPrice($new_price); 
    $quote_item->save(); 
} 
+1

感謝分享信息!所以,XML將進入我們需要創建的本地模塊,是的? (我通過搜索找到了這個:http://goo.gl/UBFgS)然後公共函數可以在運行我正在考慮的購物車代碼的phtml中聲明嗎? – 2012-05-08 21:43:56

+1

在這裏,這應該給你一個關於如何創建觀察者的概述:http://alturl.com/tja43 – 2012-05-08 22:02:23

+4

回答了我的問題!感謝分享! – haifacarina 2012-12-05 20:30:10

4

您可以使用一個觀察者類聽checkout_cart_product_add_after,並使用產品的「超級模式」設置自定義價格AGA引述報價項目。

在你/app/code/local/{namespace}/{yourmodule}/etc/config.xml:

<config> 
    ... 
    <frontend> 
     ... 
     <events> 
      <checkout_cart_product_add_after> 
       <observers> 
        <unique_event_name> 
         <class>{{modulename}}/observer</class> 
         <method>modifyPrice</method> 
        </unique_event_name> 
       </observers> 
      </checkout_cart_product_add_after> 
     </events> 
     ... 
    </frontend> 
    ... 
</config> 

然後在創建/應用/代碼觀察員類/本地/ {命名空間}/{yourmodule} /Model/Observer.php

<?php 
     class <namespace>_<modulename>_Model_Observer 
     { 
      public function modifyPrice(Varien_Event_Observer $obs) 
      { 
       $customPrice = Mage::getSingleton(’core/session’)->getCustomPriceCalcuation(); // Provide you price i have set with session 
       $p = $obs->getQuoteItem(); 
       $p->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice); 
      } 

     } 
2

如用右手由革順Herczeg,爾根Thelen和古典的xman回答。您需要編寫一個sales_quote_add_item事件的觀察者。 當任何產品添加到購物車時,您的觀察者將被觸發。如果產品是可配置的,那麼這個事件將被觸發兩次,您將不得不這樣做,以獲得簡單的產品。

$item = $observer->getEvent()->getQuoteItem(); 
    $quote = $item->getQuote(); 
    $product = $item->getProduct(); 
    if ($product->getTypeId() != "configurable") { 
     //Do your thing here 
    } 
2

有關可配置產品的問題已解決。您只需刪除

$ quote_item-> save();

然後爲產品不會添加到購物車兩次。但是這個功能仍然存在很嚴重的問題。也就是說,使用此功能,我們可以更新購物車中的商品價格,但添加到購物車後,產品價格不會根據不同的貨幣進行更改。所以這個功能不能用於有多種貨幣的商店。

如果有人發現關於這個問題的任何解決方案,請與我們分享......

3

一應俱全。

文件:/app/etc/modules/config.xml

<?xml version="1.0" encoding="UTF-8"?> 
<config> 
    <modules> 
    <Ajax_ProductAdjust> 
     <codePool>local</codePool> 
     <active>true</active> 
    </Ajax_ProductAdjust> 
    </modules> 
</config> 

文件:/app/code/local/Ajax/ProductAdjust/etc/config.xml

<?xml version="1.0"?> 
     <config> 
     <modules> 
     <Ajax_ProductAdjust> 
      <version>1.0.1</version> 
     </Ajax_ProductAdjust> 
     </modules> 
     <global> 
      <models> 
      <Ajax_ProductAdjust> 
       <class>Ajax_ProductAdjust_Model</class> 
      </Ajax_ProductAdjust> 
      </models> 
      <events> 
       <sales_quote_add_item> 
        <observers> 
        <ajax_productadjust_model_observer> 
         <type>singleton</type> 
         <class>Ajax_ProductAdjust_Model_Observer</class> 
         <method>updatePrice</method> 
        </ajax_productadjust_model_observer> 
       </observers> 
       </sales_quote_add_item> 
      </events> 
     </global> 
    </config> 

文件: /app/code/local/Ajax/ProductAdjust/Model/Observer.php

<?php 
//Notes 
class Ajax_ProductAdjust_Model_Observer 
{ 

    public function _construct() 
     { 
     } 

    public function getNewPrice() 
     { 
     //Your new functionality here 
     // 
     $newprice = ""; 

     return $newprice; 
     } 

    public function updatePrice(Varien_Event_Observer $observer) 
    { 
     $event = $observer->getEvent(); 
     $quote_item = $event->getQuoteItem(); 
     $new_price = $this->getNewPrice(); 
     $quote_item->setOriginalCustomPrice($new_price); 
     $quote_item->save(); 
     } 
} 

乾杯,

+1

如何獲得此觀察者的產品詳細信息? – Naveenbos 2015-05-05 12:15:33

+0

我嘗試過使用這種方法,並且不斷收到錯誤:'1452無法添加或更新子行:外鍵約束失敗' – NotJay 2016-02-09 21:20:37

1

這個問題並沒有說明是否應該通過向代碼中添加一些邏輯來完成。因此,既然你有開發者的答案,那麼也有一些被稱爲購物車價格規則的東西(在管理面板中去促銷>購物車價格規則),你可以創建不同的規則來製作銷售和折扣(帶或不帶優惠券)。

0
To change product price while adding product to cart, use an observer event. 
    Follow the steps given below 
    1. Add an observer in your module config.xml file. 
    2. Create an observer file in your model 
    3. add checkout_cart_product_add_after event 

文件:應用程序/代碼/本地/命名空間/模塊的/ etc/config.xml中

例如:應用程序/代碼/本地/ MGS/Rileytheme的/ etc/config.xml中

 <frontend> 
      <routers> 
       <routeurfrontend> 
        <use>standard</use> 
        <args> 
        <module>MGS_Rileytheme</module> 
        <frontName>rileytheme</frontName> 
        </args> 
       </routeurfrontend> 
      </routers> 
      <layout> 
      <updates> 
      <rileytheme> 
       <file>rileytheme.xml</file> 
      </rileytheme> 
      </updates> 
      </layout> 
      <events> 
       <checkout_cart_product_add_after> 
       <observers> 
        <rileytheme> 
        <class>rileytheme/observer</class> 
        <method>modifyPrice</method> 
        </rileytheme> 
       </observers> 
       </checkout_cart_product_add_after> 
      </events></b> 
    <frontend> 

文件:應用程序/代碼/本地/ MGS/Rileytheme /型號/ Observer.php

class MGS_Rileytheme_Model_Observer { 
    public function modifyPrice(Varien_Event_Observer $observer) { 
     //$order = Mage::registry('current_order'); For getting current order 
     //$orderid = $order->getRealOrderId(); For getting orderid 
      $quote_item = $observer->getQuoteItem(); 
      $payment = 30; //add your custom product price here and do your logic here 
      $quote_item->setOriginalCustomPrice($payment); 
      $quote_item->save(); 
      return $this; 
    } 
} 
相關問題