Override Fn_Format_Price Function

Hello, I need to round the price before it gets displayed. The issue is I cant create an addon to override fn_format_price function in the fn.common.php file. It worked when I directly edit the function in the file, but I don't wanna edit core files. What's the solution?

I tried creating an addon and install it but didn't work

init.php:

fn_register_hooks(
'fn_format_price'
);

func.php


function fn_round_prices_fn_format_price($price = 0, $currency .....

Hello

In function fn_format_price you have only 1 hook "format_price_pre" and you can refer to this hook (no hook post what could be the problem)

The name function should have name fn_"addon_id"_format_price_pre.

If you want change value in hooks you should precede the variable char "&"

e.g.

function fn_"addon_id"_format_price_pre(&$price, $currency ......

Best regards

Robert

Yes you can use 'format_price_pre' hook like:

function fn_my_changes_format_price_pre(&$price, $currency) {
  // if you want currency dependent
  if( $currency == 'usd' ) {
    // do whatever
  }
  // to round up
  $price = ceil($price);
  // to round down
  $price = floor($price);
  // Or you can do custom rounding here.
  $price = my_custom_price_adjustment($price, $currency);
}

I had to do this for a Dutch customer given they don't have 'pennies' so needed to round to appropriate 'nickle'.

Thanks a lot, my issues was the & sign

Hello

You are welcome :)

Best regards

Robert

one more thing, I have two currencies, when I need to round my currency, the $currency value in the format_price_pre function is showing a blank value ' '.
why is that?

Because some cs-cart functions do not pass the currency. In that case you should do something like:

if( empty($currency) )
  $currency = CART_PRIMARY_CURRENCY;

Now we only have two currencies, and I'm rounding the secondary currency. But later if we had more currencies, I wanna round only one of them. How can I get which currency is being passed to the format_price_pre function?

It is in the currency parameter. If it's empty, it is using the primary currency. if not, it is set to the current customer currency.

Now we only have two currencies, and I'm rounding the secondary currency. But later if we had more currencies, I wanna round only one of them. How can I get which currency is being passed to the format_price_pre function?

Try the following condition

if ($currency == CART_PRIMARY_CURRENCY) {
   ...code for primary currency only....
}

Ecom, that won't work for the condition where the $currency value is empty (which it many times is). Hence the conditional statement would need to be something akin to:

if( empty($currency) || $currency == CART_PRIMARY_CURRENCY )

Ecom, that won't work for the condition where the $currency value is empty (which it many times is). Hence the conditional statement would need to be something akin to:

if( empty($currency) || $currency == CART_PRIMARY_CURRENCY )

Look like it cannot be empty since it has default value ( CART_PRIMARY_CURRENCY )

That must be a recent change then....

In my case, the price I'm rounding is not primary, and yet the currency in the function gives an empty currency, why?

In my case, the price I'm rounding is not primary, and yet the currency in the function gives an empty currency, why?

As stated above, on older versions, the currency parameter had a default value of '' versus now it's CART_PRIMARY_CURRENCY.

If it's empty (I.e. "") then you can assume the current call is using the CART_PRIMARY_CURRENCY. See #11 above.