Last active
March 10, 2020 10:32
-
-
Save nshkuro/5829377 to your computer and use it in GitHub Desktop.
Drupal Commerce - Price without the decimal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implementation hook_commerce_currency_info_alter() | |
*/ | |
function MODULE_commerce_currency_info_alter(&$currencies, $langcode) { | |
$currencies['RUB']['format_callback'] = 'MODULE_commerce_currency_format'; | |
} | |
function MODULE_commerce_currency_format($amount, $currency, $object = NULL, $convert = TRUE) { | |
// Format the price as a number, use 0 for no decimal places in output | |
$price = number_format(commerce_currency_round(abs($amount), $currency), 0, $currency['decimal_separator'], $currency['thousands_separator']); | |
// Establish the replacement values to format this price for its currency. | |
$replacements = array( | |
'@code_before' => $currency['code_placement'] == 'before' ? $currency['code'] : '', | |
'@symbol_before' => $currency['symbol_placement'] == 'before' ? $currency['symbol'] : '', | |
'@price' => $price, | |
'@symbol_after' => $currency['symbol_placement'] == 'after' ? $currency['symbol'] : '', | |
'@code_after' => $currency['code_placement'] == 'after' ? $currency['code'] : '', | |
'@negative' => $amount < 0 ? '-' : '', | |
'@symbol_spacer' => $currency['symbol_spacer'], | |
'@code_spacer' => $currency['code_spacer'], | |
); | |
return trim(t('@code_before@code_spacer@negative@symbol_before@price@symbol_spacer@symbol_after@code_spacer@code_after', $replacements)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment