Magento Event Observer Change Cart Base Subtotal

Magento Event Observer Change Cart Base Subtotal

October 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Event Observer Change Cart Base Subtotal

Changing Magento Cart Base Subtotal Using an Event Observer

Hello! I understand that you’re trying to modify the base subtotal of your cart using the checkout_cart_save_after event in Magento. Let’s break down this process step by step.

Understanding the Problem

The core issue lies in how Magento handles prices and currency conversions. The base_subtotal is used for multi-currency setups, which requires some careful handling.

Your current approach:

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

While this code sets the base subtotal to 0, it doesn’t account for other aspects of the cart calculation.

The Correct Approach

To solve this problem correctly, we need to understand that a quote in Magento is an object that holds all the items being added to the shopping cart. It has several methods for managing the data:

  • getItems(): Returns an array of quote items.
  • setBaseSubtotal($amount): Sets the base subtotal for all items with currency conversion applied.

To change the subtotal dynamically, you should iterate through each item in the quote and update its price individually. Here’s how to do it:

public function changeCartSubtotal($observer)
{
    $quote = $observer->getEvent()->getQuote();
    foreach ($quote->getAllVisibleItems() as $item) {
        // This will set a new price for each item in the cart.
        $item->setData('price', 0);
    }
    $quote->save();
}

In this code, I’m using the changeCartSubtotal method to iterate through all visible items in the quote. For each item, it sets the price to 0.

Dynamic Pricing

If you want to dynamically change the price instead of always setting it to 0, you can use Magento’s pricing models:

  • salesRule: Used for creating discount rules.
  • taxRule: Used for tax calculations.
  • customerGroupPrice: Used for setting prices based on customer groups.

For example, if you wanted to set a specific price for an item based on the current date, you could do something like this:

if (date('m') == 12) { // If it's December
    $price = 50;
} else {
    $price = 100;
}
$item->setData('price', $price);

Conclusion

Changing the price of items in a Magento cart requires understanding how quotes and pricing work within the system. By using an event observer, you can intercept the checkout_cart_save_after event and modify the subtotal or prices as needed.

Remember to flush your cache after making changes:

Mage::app()->cleanCache();

If you need more advanced functionality like applying discounts or taxes dynamically, I recommend checking out the Laravel Company blog for in-depth tutorials on Magento development.

Checkout Extensions Customers Pricing

Our network