dshkmamau65777662 2014-02-08 17:09
浏览 57
已采纳

在保存订单之前更新magento cart total

I need to remove some products from cart after the customer press the "Place order" button. I can remove the products with a custom event and observer and update the cart products, but i cant update the cart total. In the observer i try to update the price with:

    $granTotal = $cart->getGrandTotal() - $cart->getShippingAmount();
    $cart->setGrandTotal($GranTotal - $sum)->save();

    $baseGranTotal = $cart->getBaseGrandTotal() - $cart->getShippingAmount();
    $cart->setBaseGrandTotal($baseGranTotal - $sum)->save();

    $cart->setTotalsCollectedFlag(false)->collectTotals();
    $cart->save();

But the total of the cart don't change. I find that the total is stored in the cart address too and used to set the cart total in Mage_Sales_Model_Quote collectTotals() function. Here:

$this->setSubtotal((float) $this->getSubtotal() + $address->getSubtotal());
$this->setBaseSubtotal((float) $this->getBaseSubtotal() + $address->getBaseSubtotal());

Then i tried to change the address total before calling the collectTotals(), but the cart total still don't change.

My custom event is triggered at the beginning in Mage_Checkout_Model_Type_Onepage saveOrder() function.

I am running out of ideas of how to update the total of the cart. My observer looks like this:

public function changeCart(){
    $cart = Mage::getSingleton('checkout/session')->getQuote();
    $data = array();
    $inquiry = array();
    $sum = 0;
    //remove out of stock items to save them in custom model
    foreach ($cart->getAllItems() as $item) {
        if($item->getProduct()->getData('is_in_stock') == 0){
            $cart->removeItem($item->getItemId())->save();
            $inquiry[] = $item->getProduct();
            $data['qty'][$item->getProduct()->getId()] = $item->getData('qty');
            $sum += $item->getProduct()->getFinalPrice();
            $dobavljivost = $item->getProduct()->getResource()->getAttributeRawValue($item->getProduct()->getId(), 'dobavljivost', Mage::app()->getStore());
            Mage::getSingleton('core/session')->setDobavljivost($dobavljivost);
        }
    }

    foreach($cart->getAllAddresses() as $a){
        $a->setGrandTotal(0);;
        $a->setBaseGrandTotal(0);
        $a->setData('subtotal', 0);
        $a->setData('base_subtotal', 0);
    }

    $granTotal = $cart->getGrandTotal() - $cart->getShippingAmount();
    $cart->setGrandTotal($GranTotal - $sum)->save();

    $baseGranTotal = $cart->getBaseGrandTotal() - $cart->getShippingAmount();
    $cart->setBaseGrandTotal($baseGranTotal - $sum)->save();

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

Thank you for any help!

  • 写回答

1条回答 默认 最新

  • douyi1966 2014-02-08 20:31
    关注

    For recomputing totals of the cart you can try do this

    $cart = Mage::getSingleton('checkout/session')->getQuote();
    
    foreach ($cart->getAllAddresses() as $address)
    {
       $address->unsetData('cached_items_nonnominal');
       $address->unsetData('cached_items_nominal');
    }
    
    $cart->setTotalsCollectedFlag(false);
    $cart->collectTotals();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分