【发布时间】:2012-04-01 01:41:21
【问题描述】:
我想在将该产品添加到购物车时更改产品价格。
怎么可能让我知道...
【问题讨论】:
标签: magento
我想在将该产品添加到购物车时更改产品价格。
怎么可能让我知道...
【问题讨论】:
标签: magento
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>
文件:app/code/local/MGS/Rileytheme/Model/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;
}
}
【讨论】:
问题没有说明是否应该通过在代码中添加一些逻辑来完成此操作。因此,由于您有开发人员的答案,因此还有一些称为购物车价格规则的东西(在管理面板中转到促销 > 购物车价格规则),您可以在其中创建不同的销售和折扣规则(有或没有优惠券)。
【讨论】:
坚果汤。
文件:/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();
}
}
干杯,
【讨论】:
1452 Cannot add or update a child row: a foreign key constraint fails
关于可配置产品的问题已经解决。您只需删除
$quote_item->save();
然后对于该产品将不会添加到购物车两次。但是另一个非常严重的问题仍然存在于此功能中。也就是说,通过这个功能,我们可以更新购物车中的商品价格,但添加到购物车后,产品价格不会根据不同的货币而改变。所以这个功能不能用于多币种的商店。
如果有人在该问题上找到任何解决方案,请与我们分享...
【讨论】:
正如 Gershon Herczeg、Jürgen Thelen 和 Xman Classical 的正确回答。您将需要编写一个 sales_quote_add_item 事件的观察者。 当任何产品添加到购物车时,您的观察者将被触发。如果产品是可配置的,那么这个事件将被触发两次,你将不得不这样做才能获得简单的产品。
$item = $observer->getEvent()->getQuoteItem();
$quote = $item->getQuote();
$product = $item->getProduct();
if ($product->getTypeId() != "configurable") {
//Do your thing here
}
【讨论】:
您可以使用观察者类来监听 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 创建一个 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);
}
}
【讨论】:
这样做的方法是添加一个寻找此事件的观察者'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();
}
【讨论】:
$quote_item->save(); 有不需要的行为。由于您的问题已被接受,您是否可以在答案中添加一条说明,在某些情况下您必须删除此 save ?