Magento update subtotal item

2019-08-16 08:40发布

问题:

I can't update subtotal item in cart. I created module with observe checkout_cart_product_add_after I can get subtotal price from item:

$subtotal = Mage::getSingleton('checkout/cart')->getQuote()->getSubtotal();

but I can't update this, for exmaple:

$subtotal = $subtotal + 100;
Mage::getSingleton('checkout/session')->getQuote()->setSubtotal($subtotal);
Mage::getSingleton('checkout/cart')->getQuote()->setSubtotal($subtotal);
Mage::getSingleton('checkout/session')->getQuote()->save();
Mage::getSingleton('checkout/cart')->getQuote()->save();

EDIT

If I run in my Observer print_r($subtotal); exit; I get correct updated subtotal. In cart page I have still orginal subtotal without change.

EDIT 2 I'm trying run modifySubtotal function with sales_quote_collect_totals_after event, but I can't see on cart page updated subtotal price. Below code of modifySubtotal from Observer.php:

public function modifySubtotal(Varien_Event_Observer $observer)
    {

    $session = Mage::getSingleton('checkout/session');
    $quote=$observer->getQuote();
    $subtotal = $quote->getBaseSubtotal();
    $subtotal = $subtotal +123;
    $quote->setBaseSubtotal($subtotal);

    $quote->save();
    $subtotal2 = $quote->getBaseSubtotal();
    //print_r($subtotal2);exit;

}

I'll be grateful for any tips and help.

回答1:

You should observe sales_quote_collect_totals_after.



回答2:

Issue with your code is here:

$session = Mage::getSingleton('checkout/session');
$quote=$observer->getQuote();

This needs to be

$session = Mage::getSingleton('checkout/session');
$quote=$session->getQuote();

Hope this helps



回答3:

Issue solved. I override and copy Subtotal.php from Tax/Module/Sales/Qoute/Total to /app/local/Mage/... Near 545 line I calculated my custom $subtotal value and set $item->setRowTotal($subtotal);This is all.