Update cart item price after collect totals magento2

Update cart item price after collect totals magento2

July 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Update cart item price after collect totals magento2

Updating Cart Item Price After Collect Totals In Magento 2

In Magento 2, updating cart item prices based on quote grand total can be challenging when you need to ensure the database values are accurate. You’re right that changing the price before the cart is saved doesn’t always update the database correctly.

Understanding the Issue with Current Approach

Your current approach uses an observer for the checkout_cart_save_before event, which means you’re attempting to modify item prices just before the cart is saved. The issue lies in how Magento handles its internal processes during the save operation.

When you call $quote->collectTotals(), Magento performs various calculations and updates on the items, including pricing information. However, by setting custom prices directly, you’re overwriting some of these calculated values, leading to inconsistencies between the displayed price and the database values after the save operation is complete.

A More Reliable Approach: After Cart Save

Instead of modifying prices before the cart is saved, consider using an observer for the checkout_cart_save_after event. This allows you to work with the updated data after Magento has finished its internal processing.

Here’s how you can modify your approach:

  1. Update events.xml

Change your event subscription to catch the checkout_cart_save_after event instead of checkout_cart_save_before:

<event name="checkout_cart_save_after">
    <observer name="Vendor::save_cart" instance="Vendor\Observer\AfterCartSaveObserver" shared="false"/>
</event>
  1. Update Your Observer

Your observer should now reflect the changes made by Magento after it has processed and saved the cart:

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $cart = $observer->getCart();
    $quote = $this->checkoutSession->getQuote();

    if ($quote->getGrandTotal() > 240) {
        foreach ($quote->getAllVisibleItems() as $item) {
            // Set item price to 0 for items with a grand total over $240
            $item->setPrice(0)->setBaseOriginalPrice($item->getProduct()->getPrice());
            $item->calcRowTotal();
        }
    } else {
        foreach ($quote->getAllVisibleItems() as $item) {
            // Set item price to default for items with a grand total of $240 or less
            $defaultProductPrice = $item->getProduct()->getPrice();
            $item->setPrice($defaultProductPrice)->setBaseOriginalPrice($item->getProduct()->getPrice());
            $item->calcRowTotal();
        }
    }

    return $this;
}

Why This Works Better

By observing the checkout_cart_save_after event, you’re working with the cart after Magento has finished all its internal processing and database updates. This ensures that the prices you set in your observer will be reflected accurately in both the displayed values and the database.

The key is to let Magento handle its own pricing calculations first ($quote->collectTotals()), then modify the final results as needed using calcRowTotal(). This approach respects Magento’s internal processes while still allowing for custom logic based on the grand total.

Conclusion

In Magento 2, updating cart item prices depends on quote grand total can be a delicate task. The key is to use the right event observer—checkout_cart_save_after instead of checkout_cart_save_before.

This approach respects Magento’s internal pricing mechanisms while still allowing for custom logic based on the grand total. It ensures that both the displayed values and the database remain consistent after the cart is saved.

Remember, when it comes to Magento development, understanding the flow of events and processes is crucial for creating reliable and efficient solutions.

For more insights into Magento 2 development best practices, feel free to explore our comprehensive guides at Laravel Company.

Happy coding!

Magento 2 Checkout Database Extensions

Our network