Magento:通过事件以编程方式更新购物车 [英] Magento: Programatically update cart via event

查看:70
本文介绍了Magento:通过事件以编程方式更新购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我迷上了sales_quote_save_after事件分配器.首先,作为测试,我试图将商品放入购物车并将其价格更改为0.这样做之后,我将使用其他一些商品.

I'm hooking into the sales_quote_save_after event disaptcher. Firstly as a test I'm trying to get the items in the cart and change their prices to 0. After I do that I'll be building in some other stuff.

我遇到了麻烦.我可以更改单价没问题,但是当我重新计算购物车总额时,价格就会恢复为原始价格.

I'm having trouble. I can change the unit price no problem, but when I go to recalculate the cart totals the prices are reverting back to the original price.

public function sales_quote_save_before($observer) {

    if (Mage::registry('basket_observer_executed')) {
        return $this;
    }

    $quote = $observer->getEvent()->getQuote();

    foreach ($quote->getAllItems() as $item) {
        if($item->getId()) {    
            $item->setCustomPrice(0);
        }
    }

    Mage::register('basket_observer_executed', true);

    $quote->save();
    $quote->setTotalsCollectedFlag(false)->collectTotals();
}

有人能指出我在重新计算总数的同时保持新价格吗?

Can anyone point me towards maintaining the new price whilst doing the recalculation of the totals?

推荐答案

我会尝试使用其他事件.我总是使用checkout_cart_save_after来执行类似的操作.使用我提到的事件,使用以下代码:

I'd try using a different event. I always use checkout_cart_save_after for doing similar actions.Using the event I mentioned, use the following code:

$cart = $observer->getData('cart');

$quote = $cart->getData('quote');

$items = $quote->getAllVisibleItems();

foreach($items as $item)
{
    $item->setCustomPrice(0);
    $item->setOriginalCustomPrice(0);
    $item->getProduct()->setIsSuperMode(true);
    $item->save();
}
$quote->save();
$quote->setTotalsCollectedFlag(false)->collectTotals();

这篇关于Magento:通过事件以编程方式更新购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆