magento customize quote collectTotals to show the updated totals

magento customize quote collectTotals to show the updated totals

August 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento customize quote collectTotals to show the updated totals

Customizing Magento Quote Totals for Discount Calculations

Hello developers! Today we’re going to dive into customizing the Magento quote totals collection process to reflect your custom discount logic accurately. We’ll walk through how to apply discounts using options, and then customize the collectTotals method to ensure your new prices are reflected in both the frontend and backend.

Applying Custom Discounts Using Options

You’re right on track with applying discounts via item options instead of relying on Magento’s built-in discount system. Let’s first look at how you can set up a custom discount option:

$item->addOption(array(
    'code' => 'promo',
    'value' => serialize(['amount' => 10, 'qty' => 1])
));

In this example, we’re adding an option with the code promo to our quote item. The value is a serialized array containing the discount amount ($amount) and the quantity at which the discount applies ($qty).

Displaying Discounts in the Frontend Cart

Now let’s display these discounts in the frontend cart page:

$promo = $item->getOptionByCode('promo');
echo '<div class="orig-price">' . $item->getOriginalPrice() . '</div>';
echo '<div class="new-price">' . ($item->getOriginalPrice() - (unserialize($promo['value'])['amount'] * unserialize($promo['value'])['qty'])) . '</div>';

Here, we’re first retrieving the promo option using getOptionByCode(). We then deserialize the value array and calculate the new price by subtracting the discount amount from the original price.

The Challenge: Updating Quote Totals

The issue you’re facing is that Magento doesn’t automatically update the quote totals to reflect your custom discounts. To fix this, we need to customize the collectTotals method in the Mage_Sales_Model_Quote class.

First, find the collectTotals method in your app/code/core/Mage/Sales/Model/Quote.php file:

public function collectTotals()
{
    // Your existing code here...
}

Now, you’ll need to loop through each item in the quote and apply your custom discount logic. Here’s a basic example of how you might do this:

public function collectTotals()
{
    foreach ($this->getAllVisibleItems() as $item) {
        if ($promo = $item->getOptionByCode('promo')) {
            $discountDetails = unserialize($promo['value']);
            $discountAmount = $discountDetails['amount'] * $discountDetails['qty'];

            // Apply the discount to the item's price
            $item->setOriginalPrice($item->getRowTotal() - $discountAmount);
        }
    }

    parent::collectTotals();
}

In this example, we’re looping through all visible items in the quote. For each item with a promo option, we deserialize the value array and calculate the discount amount based on the quantity at which it applies.

We then apply this discount to the item’s original price using setOriginalPrice().

Conclusion

Customizing the Magento quote totals collection process is a powerful way to implement complex discount logic that doesn’t fit the built-in discount system. By applying discounts via options and customizing the collectTotals method, you can ensure your new prices are reflected accurately in both the frontend cart and the backend order summary.

Remember, when working with core Magento files like Quote.php, it’s always a good practice to create an extension and override the necessary classes instead of modifying the core directly. This ensures your changes remain stable across updates.

For more on creating extensions and overriding core functionality, check out our guide: Creating Custom Magento Extensions.

Happy coding! Let me know if you have any further questions.

Checkout Admin Pricing

Our network