Magento, getSubtotal and getGrandTotal always return zero

Magento, getSubtotal and getGrandTotal always return zero

August 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento, getSubtotal and getGrandTotal always return zero

Debugging Magento: When getSubtotal and getGrandTotal Always Return Zero

Are you facing a puzzling issue where your Magento totals methods like getSubtotal() and getGrandTotal() consistently return zero despite having non-zero values in the database? Let’s dive into this challenge step-by-step.

Understanding the Total Collection Process

Magento collects subtotal, tax, discount, and grand total during checkout using the collect method of each total model. These totals are stored in the sales_flat_quote_address_item table as row_total, tax_amount, discount_amount, and grand_total.

Identifying the Core Problem

In your case, the issue seems to stem from the order of total collection. Magento processes totals in the following sequence: shipping & handling, tax, discount, subtotal, grand total.

If you haven’t configured your custom total model correctly in config.xml, it might be getting processed before the essential totals like grand total and subtotal are calculated. This leads to zero values being returned for subsequent calls.

Investigating Your collect Method

Let’s analyze the key parts of your collect method:

  1. You’re setting the base grand total without checking if it has been calculated already.
  2. The conversion from base to store currency is happening after adding the installment fee, which could be causing issues in certain edge cases.

1. Properly Integrate Your Total Model

Ensure your config.xml file includes the correct sequence for total collection:

<config>
  <global>
    <sales>
      <quote>
        <totals>
          <oos_instalment>
            <class>Oos_Instalment_Model_Total_Quote_Instalment</class>
            <before>&lt;totalgrand_total&gt;</before> <!-- Ensure this is after grand_total -->
          </oos_instalment>
        </totals>
      </quote>
    </sales>
  </global>
</config>

2. Optimize Your Total Calculation

To prevent potential issues with currency conversion, calculate the installment fee directly in base currency:

$baseTotal = $address->getBaseGrandTotal();
$store = $address->getQuote()->getStore();
$fee_perc = floatval($oosconfig['inst' . round($address->getInstalmentCount())]);
$ins_fee = $store->convertPrice($baseTotal * ($fee_perc / 100.0), true); // Calculate in base currency

3. Check for Existing Totals

Before modifying the grand total, check if it’s already been calculated:

if (!$address->getBaseGrandTotal()) {
    $address->setBaseGrandTotal(0);
}

$baseTotal = $address->getBaseGrandTotal();
...
$address->setGrandTotal($store->convertPrice($baseTotal + $ins_fee, false));

Conclusion

The key to resolving this issue lies in properly configuring your total model’s sequence and handling edge cases correctly. By ensuring your custom total is processed after the essential totals and optimizing your currency conversion logic, you can prevent incorrect zero values from being returned.

Remember, proper Magento development often requires a deep understanding of the core framework’s total collection process. Always refer to the official Magento documentation for accurate guidance: Magento 2 Developer Documentation.

Happy coding!

Checkout Database Configuration Orders

Our network