how to update cart total after change item price with observer?

how to update cart total after change item price with observer?

July 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to update cart total after change item price with observer?

Solving Subtotal Issues when Modifying Cart Prices with Observers

As a developer experienced in e-commerce systems like Magento, I’ve encountered this issue many times while working on projects for clients at Laravel Company. When you modify the price of an item in the cart using an observer, the subtotal doesn’t always update immediately due to Magento’s caching and rendering processes.

The core problem lies in how Magento handles the recalculation of totals after a product price change. The getSubtotal method is cached for performance reasons, so even though you’ve updated the custom price in your observer, the cached value remains until it’s refreshed or invalidated.

To resolve this issue and ensure your subtotal updates correctly after applying the free gift or any other price modification, follow these steps:

Step 1: Set the Custom Price

Your current code is correct for setting the new (zero) custom price on the item. The lines:

$quote_item->setOriginalCustomPrice($new_price);
$quote_item->setCustomPrice($new_price);

are properly updating the custom price fields on the quote item.

Step 2: Recollect the Totals

To invalidate the cached subtotal and force a recalculation, you need to trigger Magento’s recollectTotals method. This will update all totals for the current quote instance:

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

Step 3: Save the Quote

After applying the price changes and recalculating the totals, you must save the updated quote object to persist these modifications:

$quote->save();

This ensures that the new subtotal is saved in the session.

Here’s how your complete observer method should look:

public function modifyPrice(Varien_Event_Observer $observer)
{
    $event = $observer->getEvent();
    $quote = Mage::getModel('checkout/session')->getQuote();
    $quote_item = $event->getQuoteItem();

    // Your existing price modification logic here...

    if ($productData['gift']) {
        $new_price = 0;
        $quote_item->setOriginalCustomPrice($new_price);
        $quote_item->setCustomPrice($new_price);
        $quote_item->save();
    }

    // Invalidate cached totals and recalculate
    $quote->recollectTotals();

    // Save the updated quote
    $quote->save();
}

Step 4: Refresh the Cart Page

After applying these modifications, you need to refresh or redirect the customer to the shopping cart page for them to see the updated subtotal. Magento’s frontend will then retrieve the freshly saved totals from the session and display them correctly.

Remember that observers are triggered after an event, but before the corresponding action is executed by the controller. Therefore, it’s essential to ensure your changes take effect before returning control to the main flow of the shopping process.

Conclusion

Updating cart prices dynamically using observers in Magento requires careful handling of totals recalculation and persistence. By setting custom prices, invalidating cached totals with recollectTotals, saving the updated quote, and redirecting or refreshing the cart page, you can ensure that your subtotals update correctly even after price modifications.

At Laravel Company, we’ve seen these techniques work consistently across various projects involving complex pricing rules and promotional discounts. Don’t hesitate to explore our blog for more insights into Magento development best practices and performance optimization strategies: https://laravelcompany.com/blog.

Happy coding!

Checkout Catalog Cache Performance

Our network