将产品添加到购物车时更新小计 [英] updating subtotal when products added to cart
问题描述
我目前正以编程方式将产品添加到购物车,以在我的网站上创建免费样品"外观.
I am currently adding products programatically to the cart, to create a 'free sample' aspect to my site.
当前,所有这些产品的价格均为0美元,最多可以添加15个.购物车中有5个免费样品"产品后,我需要在小计中添加20美元.
Currently, all of these products are $0 and you can add a maximum of 15. Once you have 5 'free sample' products in the cart, I need to add $20 to the subtotal.
我知道我可以使用以下方法获得总计:
I know I can get the grandtotal using:
Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal();
但是我如何访问小计并向其中添加20美元呢?
But how do I access the subtotal and also add $20 to it?
我猜我需要写一个观察者,因为我需要检查何时从购物车中添加/删除产品,以便添加或不添加 20 美元.
I'm guessing I'll need to write an observer, as I'll need to check when products are added/removed from the cart in order to either add the $20 or not.
有人做过此事吗?或者可以为我指出正确的方向?
Has anyone done this before or could point me in the right direction of how I could do this?
非常感谢
谢谢
推荐答案
我最近实际上回答了这个问题,这里是需要做什么的基本想法,如果您需要更具体的细节,请告诉我:添加一个查找此事件"sales_quote_add_item"的观察者:
I actually answered this question recently here is a basic idea of what needs to be done, let me know if you need more specific details: Add an observer which looks for this event '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>
观察者应具有执行以下操作的方法:
The observer should have a method which does something like this:
public function updatePrice($observer) {
$event = $observer->getEvent();
$quote_item = $event->getQuoteItem();
if(sample etc...) $new_price = <insert logic>
$quote_item->setOriginalCustomPrice($new_price);
$quote_item->save();
}
这篇关于将产品添加到购物车时更新小计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!