Magento howto fetch / change / overwrite the totals.phtml calculation?

Magento howto fetch / change / overwrite the totals.phtml calculation?

July 29, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento howto fetch / change / overwrite the totals.phtml calculation?

Magento: Overwriting Totals Calculation in totals.phtml

Magento’s checkout process involves complex calculations for subtotals, taxes, and grand totals. These are handled by the totals.phtml file located at /app/design/frontend/{package}/{theme}/template/checkout/cart/totals.phtml. In this post, we’ll explore how to fetch and override these calculations, especially when dealing with custom product pricing.

Understanding Magento’s Checkout Calculation Flow

The totals.phtml file uses the blocks like tax/checkout_grandtotal, tax/checkout_subtotal, and tax/checkout_tax to render the totals. These blocks correspond to the respective PHP files under /app/design/frontend/{package}/{theme}/template/tax.

For instance, the grand total is calculated in the grandtotal.phtml file as follows:

<?= $this->helper('checkout')->formatPrice($this->getTotal()->getValue()) ?>

The getTotal() method fetches the grand total value.

Diving into the Core PHP Files

To understand where the calculations are happening, let’s look at the core files:

  1. totals.phtml: This file renders all the totals using the renderTotals method.
<table id="shopping-cart-totals-table">
    <!-- ... -->
    <tbody>
        <?= $this->renderTotals() ?>
    </tbody>
</table>
  1. Totals.php (under /app/code/core/Mage/Checkout/Block/Cart/): This block file contains the renderTotal method.
public function renderTotal($total, $area = null, $colspan = 1)
{
    // ...
}

The renderTotal method uses a renderer to display each total.

Fetching Custom Calculation Data

To fetch the custom calculation data for your product, you can use the following approach:

  1. In your totals.phtml file, override the rendering of the subtotal or grandtotal section.
  2. Inside this overridden block, fetch the required product details using the Magento model.

For example, to fetch a specific product’s special price:

<?php
$productIds = array();
$productIds[] = $_item['product_id'];

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('gl_special')
    ->addMinimalPrice()
    ->addStoreFilter()
    ->addIdFilter($productIds);

$product = $products->getItemById($_item['product_id']);
$bBerechnet = $product->getData('gl_special');
?>

This code fetches the gl_special attribute for the product in the cart item.

Overriding the Calculation

To override the total calculation, you’ll need to:

  1. Create a new totals renderer block.
  2. Define your custom logic in this new block’s renderTotal method.
  3. Override the rendering of the respective total (grandtotal/subtotal) with your new block.

For detailed instructions on creating a custom totals renderer and overriding the renders, refer to our guide at Laravel Company: Creating Custom Totals Renderers in Magento.

Conclusion

Overwriting the totals calculation in Magento requires a deep understanding of the checkout process. You need to fetch the required product data and then override the rendering of the totals using custom blocks.

Remember, Magento’s code is complex but well-organized. By following the core file structure and using the relevant models and methods, you can achieve your goal efficiently while maintaining the system’s stability and performance.

Happy coding! Let us know if you have any further questions or need more detailed guidance on a specific part of the process at Laravel Company.

Checkout Catalog Database Themes

Our network