在将产品添加到购物车时更改报价中的价格:magento

我想在将产品添加到购物车的同时更改产品价格。

它有可能让我知道

这样做的方法是添加一个观察者来查找这个事件'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(); } 

您可以使用观察员类来收听checkout_cart_product_add_after,并使用产品的“超级模式”根据报价项目设置自定义价格。

在/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> 

然后在/app/code/local/{namespace}/{yourmodule}/Model/Observer.php创build一个Observer类

  <?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); } } 

汤坚果。

文件:/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(); } } 

干杯,

正如Gershon Herczeg,JürgenThelen和Xman Classical所说的那样。 您将需要编写一个sales_quote_add_item事件的观察者。 当任何产品被添加到购物车时,您的观察者将被触发。 如果产品是可configuration的,那么这个事件将被触发两次,你将不得不这样的事情只得到简单的产品。

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

关于可configuration产品的问题已经解决。 你只需删除

$ quote_item-)>保存(;

然后为产品不会添加到购物车两次。 但是这个function还有一个非常严重的问题。 也就是说,通过这个function,我们可以更新购物车中的商品价格,但是join购物车之后,商品价格不会根据不同的货币而改变。 所以这个function不能用于有多种货币的商店。

如果有人在这个问题上find任何解决办法,请与我们分享…

这个问题并没有说明这是否应该通过添加一些逻辑的代码或不完成。 因此,既然你有开发者的答案也有一些被称为购物车价格规则(在pipe理面板去促销>购物车价格规则),您可以创build不同的规则,使销售和折扣(带或不带优惠券)。

  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 

文件:app / code / local / Namespace / Module / etc / config.xml

例如:app / code / local / 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; } }