1

UberCart for Drupal has some difficulties with currencies. However, by overriding "uc_currency_format", you can at least do some background calculation to give you a good estimate of the converted value. However, as it's part of UberCart Core, you can't edit the file, So you risk losing your code after every update. Also, this function does not have a hook!

That means the only that I can think of dealing with this, is having a module that overrides the function. So my question is...

Is there a way to override an existing PHP function? For example, I have:

function uc_currency_format($value, $sign = TRUE, $thou = TRUE, $dec = NULL) 
{
    // dont do this
}

But when this gets called, I want it to instead execute this

function uc_currency_format_rewrite($value, $sign = TRUE, $thou = TRUE, $dec = NULL) 
{
    // do this
}

Is that possible?

rockstardev
  • 13,479
  • 39
  • 164
  • 296
  • 1
    Possible duplicate of [Is it possible to overwrite a function in PHP](http://stackoverflow.com/questions/3620659/is-it-possible-to-overwrite-a-function-in-php) – Can O' Spam Jan 27 '16 at 11:11

2 Answers2

3

It seems to be one of those very rare times you need to hack the core code. When it comes to this, I try to limit the impact at minimum like this:

  • Rename original function. In your case, you would go with something like 'uc_currency_format_ORIGINAL'
  • In your custom module, rename your 'uc_currency_format_rewrite' into 'uc_currency_format'

This way, you will have your own code running.

At next update, you will see in your testing environment (always better to test, before applying updates to production sites) a duplicate function name fatal error. If your hook has not been implemented yet, you will rename the original fuction, again.

This method is not defined in the best practice, of course. Use it at your own risk.

Augusto
  • 2,125
  • 18
  • 27
2

No it is not possible to override a function in PHP. Drupal 7 does not use Zend (rename_function(), override_function()) or OOP in modules. So you could only ask the maintainer for a new hook. Maybe you could write a patch, which provides this hook and ask the maintainer for implementing it.

Huelfe
  • 1,776
  • 16
  • 25