将产品添加到购物车时更新小计

ter*_*d25 0 magento

我目前正在以编程方式将产品添加到购物车中,以便为我的网站创建"免费样本"方面.

目前,所有这些产品都是0美元,您最多可以添加15个.一旦您在购物车中有5个"免费样品"产品,我需要在小计中添加20美元.

我知道我可以使用以下方式获得格式:

Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal();
Run Code Online (Sandbox Code Playgroud)

但是如何访问小计并且还要加20美元呢?

我猜我需要写一个观察者,因为我需要检查什么时候从购物车中添加/删除产品,以便添加20美元.

有没有人之前做过这个或者能指出我如何做到这一点的正确方向?

非常感谢

谢谢

Ger*_*zeg 5

我实际上最近回答了这个问题,这里是需要做什么的基本概念,如果您需要更具体的细节,请告诉我:添加一个查找此事件的观察者'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>
Run Code Online (Sandbox Code Playgroud)

观察者应该有一个类似这样的方法:

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();
}
Run Code Online (Sandbox Code Playgroud)