| Project: | Ubercart Contributions |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | patch (needs review) |
uc_currency hook, as described: http://ubercart.org/forum/support/1465/can_ubercart_do
I'm not entirely sure this is the best way to implement it, so please comment. In particular, there can only be one function to hook - it would be possible to run a regular hook invoke, but I'm not sure what you'd do with the values, and how multiple functions should interact (if, for some reason, they exist).
I created uc_currency_format_value() so possibly uc_currency_format_hook() can call it, if it needs to.
--- uc_store.module.orig 2007-10-24 02:25:51.000000000 -0400
+++ uc_store.module 2007-10-24 01:22:28.000000000 -0400
@@ -1855,10 +1855,11 @@
return $output;
}
+
/**
* Format an amount for display with the store's currency settings.
*/
-function uc_currency_format($value, $sign = TRUE, $thou = TRUE, $dec = NULL) {
+function uc_currency_format_value($value, $sign = TRUE, $thou = TRUE, $dec = NULL) {
if (variable_get('uc_currency_prec', 2) > 0) {
if (abs($value) < '.'. str_repeat('0', variable_get('uc_currency_prec', 2) - 1) .'1') {
$value = 0;
@@ -1879,6 +1880,23 @@
if ($sign && variable_get('uc_sign_after_amount', FALSE)) {
$format .= variable_get('uc_currency_sign', '$');
}
+
+ return $format;
+}
+
+/**
+ * Format an amount for display with the store's currency settings.
+ * This function is invoked whenever a price is displayed to the user.
+ * It invokes uc_currency_format_hook(), if it exists. which can be used to
+ * convert the displayed currency to another nationality.
+ */
+function uc_currency_format($value, $sign = TRUE, $thou = TRUE, $dec = NULL) {
+ $format = uc_currency_format_value($value, $sign, $thou, $dec);
+
+ // call currency_format_hook if it exists
+ if (function_exists('uc_currency_format_hook')) {
+ $format = uc_currency_format_hook($format, $value, $sign, $thou, $dec);
+ }
return $format;
}
Sample implementation hook:
if (!function_exists('uc_currency_format_hook')) {
function uc_currency_format_hook($format, $value, $sign = TRUE, $thou = TRUE, $dec = NULL) {
return '<strike>'.$format.'</strike>';
}
}
else {
watchdog('uc_currency', t('uc_currency_format_hook() has already been defined by another module!'), WATCHDOG_WARNING);
}possible changes (up for discussion):
- invoke multiple hooks? (what is the expected behaviour when two modules implement the hook?)
- in uc_currency_format(), don't call uc_currency_format_value() if a hook is called? (eg, should hook should do all the work?)


I haven't considered this since then, and it never got produced for core inclusion. At the moment, I think we're a little late in the game to add a new hook to core, but if you work out a patch that we can work with in the future that would be awesome.