In Magento, I want to apply a custom PHP function to the display of prices on the frontend without changing the actual numerical price on the backend / shopping cart.
To be specific, I want to remove decimals on the display price when there are no cents in the price. For example, $19.00 would display as $19 but $19.99 would display as $19.99.
I found a PHP function on PHP.net that will perform this change for me:
// formats money to a whole number or with 2 decimals; includes a dollar sign in front
function formatMoney($number, $cents = 1) { // cents: 0=never, 1=if needed, 2=always
if (is_numeric($number)) { // a number
if (!$number) { // zero
$money = ($cents == 2 ? '0.00' : '0'); // output zero
} else { // value
if (floor($number) == $number) { // whole number
$money = number_format($number, ($cents == 2 ? 2 : 0)); // format
} else { // cents
$money = number_format(round($number, 2), ($cents == 0 ? 0 : 2)); // format
} // integer or decimal
} // value
return $money;
} // numeric
} // formatMoney
I do not want to have to change Magento templates to apply this function, because the prices appear all over the place. It would be a nightmare to update all the templates.
I would like to know whether there is a place I can use this function to format the display of the price globally so it affects the display of all prices everywhere from one spot.
Already I have spent a few hours poking around various Magento files, including:
app/code/core/Mage/Directory/Model/Currency.php
public function format <-- This function changes the real price, not the display of the price.
app/code/core/Mage/Catalog/Model/Product.php:
public function getFormatedPrice <-- This function looked promising but didn't do anything for me.
I also looked at these files without anything jumping out as an obvious place:
app/code/core/Mage/Catalog/Block/Product.php
app/code/core/Mage/Catalog/Block/Product/Price.php
app/code/core/Mage/Catalog/Block/Product/View.php
Do you think it's possible to find one place in Magento that I can hack to apply my custom PHP function to the display of the price (and not the actual numerical price in the shopping cart)?