Rounding Subtotal, Tax, and Total in Magento
Rounding prices and totals to a specific decimal place is crucial for providing a consistent shopping experience to your customers. In this post, we’ll explore how to round the subtotal, tax, and grand total values in Magento to 2 decimal places.
The Problem with Half Cents
Displaying prices and totals with half cents (e.g., $19.50) can be confusing for customers. It may suggest that you’re charging fractions of a cent, which is not standard practice in most currencies. This inconsistency can negatively impact the customer’s perception of your store.
Rounding the Grand Total
You’ve already identified the correct file to modify - app/code/core/Mage/Sales/Model/Quote/Address/Total/Grand.php. The key function here is collect(), which calculates the grand total and base grand total for a given quote address.
Here’s the modified version of the function with rounding applied:
public function collect(Mage_Sales_Model_Quote_Address $address)
{
$grandTotal = $address->getGrandTotal();
$baseGrandTotal = $address->getBaseGrandTotal();
$totals = array_sum($address->getAllTotalAmounts());
$baseTotals = array_sum($address->getAllBaseTotalAmounts());
$address->setGrandTotal(round(($grandTotal + $totals), 2));
$address->setBaseGrandTotal(round(($baseGrandTotal + $baseTotals), 2));
return $this;
}
In this code, we’re using the round() function to round the sum of the grand total and all other totals to 2 decimal places. We apply the same logic for both the total amount and its base currency equivalent.
Rounding the Subtotal and Tax
To simultaneously round the subtotal and tax, we need to modify the relevant files in the Total directory:
Subtotal.php: For rounding the subtotal.Tax.php: For rounding the tax amounts.
Here’s how you can achieve this:
Rounding the Subtotal
In app/code/core/Mage/Sales/Model/Quote/Address/Total/Subtotal.php, modify the collect() function as follows:
public function collect(Mage_Sales_Model_Quote_Address $address)
{
if ($this->_canApplyAmount($address)) {
$subtotal = 0;
foreach ($address->getAllItems() as $item) {
if (!$item->isDeleted()) {
$rowTotal = $item->getRowTotal();
$subtotal += round($rowTotal, 2);
}
}
$address->setSubtotal($subtotal);
}
return $this;
}
We’re calculating the subtotal by summing up the rounded row totals of all items in the quote address.
Rounding the Tax
In app/code/core/Mage/Sales/Model/Quote/Address/Total/Tax.php, modify the collect() function as follows:
public function collect(Mage_Sales_Model_Quote_Address $address)
{
$taxAmount = 0;
foreach ($this->_getApplicableTaxes($address) as $item) {
if (isset($item['percent']) && isset($item['tax_amount'])) {
$taxAmount += round($item['tax_amount'], 2);
}
}
$address->setTaxAmount($taxAmount);
return $this;
}
We’re summing up the rounded tax amounts for all applicable taxes and setting the total tax amount for the quote address.
Conclusion
Rounding prices and totals to a consistent decimal place is essential for maintaining a professional appearance in your Magento store. By modifying the relevant files and functions, you can ensure that your customers see clear and predictable pricing throughout their shopping experience.
Remember to test these modifications thoroughly after applying them to your live site. If you encounter any issues or need further assistance, don’t hesitate to reach out to our team at Laravel Company.
Happy coding!