dsk49208 2013-01-16 16:09
浏览 22
已采纳

Magento:通过事件以编程方式更新购物车

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?

  • 写回答

1条回答 默认 最新

  • doujiao7520 2013-01-16 18:42
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答