Calculate shipping cost based on Grand Total of basket?

Calculate shipping cost based on Grand Total of basket?

August 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Calculate shipping cost based on Grand Total of basket?

Calculating Shipping Costs: A Guide for Magento Developers

As an experienced e-commerce developer, I’ve often been asked how to calculate shipping costs based on the grand total of a basket in Magento. The default behavior calculates shipping based on the subtotal before tax, which can lead to confusion or miscalculations if your store imposes significant taxes.

Understanding the Default Behavior

In Magento’s core, the shipping calculation is handled by the getPrice method in the Mage_Shipping_Model_Rate_Request class. This method retrieves the sub total of the order items before tax using $order->getSubtotal().

Therefore, the shipping rates you define in your Table Rates are always calculated against this subtotal amount.

Modifying the Shipping Calculation

To change this behavior, we need to override the getPrice method and instead calculate the grand total (including tax) for each item. Here’s a step-by-step guide:

  1. Create a custom module using the Magento Module Creator or manually in your app/code/local directory.

  2. In the config.xml file of your module, declare an observer that listens to the sales_model_service_quote_address_collect_totals_before event. This fires before the shipping rates are collected.

  3. In your observer class, override the protected $subtotal and $grand_total properties of the quote object using the following code:

public function observe($observer)
{
    $quote = $observer->getEvent()->getQuote();
    foreach ($quote->getAllItems() as $item) {
        if (!$item->isDummy()) {
            $item->setSubtotal($item->getTotal());
            $item->setGrandTotal($item->getRowTotal());
        }
    }
    return $this;
}

This code loops through each item in the quote, marking it as non-dummy (not a tax or shipping item), and sets its subtotal to its total price and grand total to its row total.

  1. Reindex your catalog and flush your cache after installing this module.

Conclusion

By following these steps, you’ve successfully modified Magento’s core behavior to calculate shipping rates based on the grand total of each item, including taxes. This ensures that your customers always see accurate shipping costs at checkout.

Remember, modifying core files directly is generally not recommended for security and upgrade reasons. Using an observer and a custom module allows you to override this behavior cleanly while maintaining Magento’s upgrade path.

If you need further assistance with Magento development or e-commerce solutions, feel free to contact us at Laravel Company. We specialize in providing high-quality, secure, and performant Magento services for businesses of all sizes.

Database Extensions Configuration Orders

Our network